function getCounter(){
var num = 0;
return function(){
return num++;
};
}
How about overriding default functions? This is a snippet from a codebase I'm working on right now:
require(['helpers/user'], function(User){
// hack because we are not using layout.master
User.isLoggedIn = function(){ return global.userInfo.id > 0; };
});
How about re-using some code in a function? Another snippet from the same code, where self can change based on context:
How about simple closures:
How about overriding default functions? This is a snippet from a codebase I'm working on right now: How about re-using some code in a function? Another snippet from the same code, where self can change based on context: These are simply different ways to use closures, but that's part of the benefit of using anonymous functions.