Created
July 6, 2011 04:05
-
-
Save eliperelman/1066543 to your computer and use it in GitHub Desktop.
Closures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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