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