Skip to content

Instantly share code, notes, and snippets.

@Razenbull
Last active August 29, 2015 14:25
Show Gist options
  • Save Razenbull/4307c6fd8ab8b2e89140 to your computer and use it in GitHub Desktop.
Save Razenbull/4307c6fd8ab8b2e89140 to your computer and use it in GitHub Desktop.
counter
function counter() {
var count = 1;
return function() {
alert(count++);
}
}
Each can increment count variable despite the fact that we're outside the scope of the count() function.
var foo = {}, bar = {};
foo.count = counter();
bar.count = counter();
foo.count(); // alerts "1"
bar.count(); // alerts "1"
foo.count(); // alerts "2"
bar.count(); // alerts "2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment