Skip to content

Instantly share code, notes, and snippets.

@eliperelman
Created July 6, 2011 04:05
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 eliperelman/1066543 to your computer and use it in GitHub Desktop.
Save eliperelman/1066543 to your computer and use it in GitHub Desktop.
Closures
var factoryAdder = function(initialValue) {
// this function "closes over" initialValue,
// effectively sealing it into the context
return function(numberToAdd) {
return initialValue + numberToAdd;
};
};
var add5 = factoryAdder(5);
console.log(add5(7)); // logs 12
var add100 = factoryAdder(100);
console.log(add100(add5(20))); // logs 125
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment