javascript: clone a function by assigning it to a variable -


why can't clone function in javascript assigning variable?

e.g:

var $ = document.getelementbyid; 


usage attempt:

typeof $;  //--> "function" $('nav');  //--> "typeerror: illegal invocation" 


think duplicate function, , still callable. can explain why not?

when assigning document.getelementbyid variable lose this === document part you'd have when calling method of document. avoid this, use .bind() explicitly set this context function uses:

var $ = document.getelementbyid.bind(document); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -