Skip to content

Instantly share code, notes, and snippets.

@jgautier
Created February 22, 2011 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgautier/838239 to your computer and use it in GitHub Desktop.
Save jgautier/838239 to your computer and use it in GitHub Desktop.
var addNumber=function(x){
//first class function : a function treated as an object
return function(y){
//x is a free variable. (i.e. not a local variable or an argument of this function.)
//x is bound to the argument x in lexical environment defined by the addNumber function
return x+y;
};
};
var foo="bar";
$("#aButton").click(function(){//passing a function in as an object (i.e. first class function)
alert(foo);//foo is bound in the global lexical environment
});
var myModule=(function(){
var x=1;
return getX(){//return a function as an object (i.e. first class function)
return x;//x is bound to the lexical environment defined by the outer function
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment