Skip to content

Instantly share code, notes, and snippets.

@jessegilbride
Created April 8, 2016 22:14
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 jessegilbride/0cd2bedb2c90c7b98b73304391094c18 to your computer and use it in GitHub Desktop.
Save jessegilbride/0cd2bedb2c90c7b98b73304391094c18 to your computer and use it in GitHub Desktop.
A fun way to think about JS closures. From http://www.hongkiat.com/blog/javascript-jargon/
function order() {
var food;
function waiter(order) {
chef(order);
return food;
}
function chef(order) {
if (order === 'pasta') {
food = ['pasta', 'gravy', 'seasoning'];
cook();
}
}
function cook() { food.push('cooked'); }
return waiter;
}
var myOrder = order();
console.log(myOrder('pasta'));
// Array [ "pasta", "gravy", "seasoning", "cooked" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment