Skip to content

Instantly share code, notes, and snippets.

@mde
Created May 19, 2015 23:33
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 mde/8c5c8a3cd48d6c2dd545 to your computer and use it in GitHub Desktop.
Save mde/8c5c8a3cd48d6c2dd545 to your computer and use it in GitHub Desktop.
JavaScript closures
var foo = function (seed) {
var currVal = seed;
var incrementor = function () {
currVal++;
console.log(currVal);
};
return incrementor;
};
var bar = foo(3);
bar(); // Logs 4
bar(); // Logs 5
bar(); // Logs 6
var baz = function (seed) {
var firstFactor = seed;
var multiplier = function (secondFactor) {
console.log(firstFactor * secondFactor);
};
return multiplier;
};
var qux = baz(3);
qux(4); // Logs 12
qux(7); // Logs 21
qux(10); // Logs 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment