Skip to content

Instantly share code, notes, and snippets.

@skeggse
Last active December 26, 2015 05:59
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 skeggse/7104450 to your computer and use it in GitHub Desktop.
Save skeggse/7104450 to your computer and use it in GitHub Desktop.
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