Created
April 8, 2016 22:14
-
-
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/
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
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