Last active
December 26, 2015 05:59
-
-
Save skeggse/7104450 to your computer and use it in GitHub Desktop.
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 numApples = 5; | |
function handleConn(name) { | |
var pickedApples = 0; | |
return function pick() { | |
pickedApples++; | |
numApples--; | |
console.log(name, 'has', pickedApples, 'apples'); | |
}; | |
} | |
// fake client connects | |
var tim = handleConn('tim'); | |
var john = handleConn('john'); | |
tim(); // tim has 1 apples | |
tim(); // tim has 2 apples | |
john(); // john has 1 apples | |
console.log(numApples); // 2 | |
setTimeout(function() { | |
tim(); // tim has 3 apples | |
console.log(numApples); // 1 | |
}, 500); | |
setTimeout(function() { | |
john(); // john has 2 apples | |
console.log(numApples); // 0 | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment