Last active
August 29, 2015 14:25
-
-
Save Razenbull/4307c6fd8ab8b2e89140 to your computer and use it in GitHub Desktop.
counter
This file contains hidden or 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 counter() { | |
var count = 1; | |
return function() { | |
alert(count++); | |
} | |
} |
This file contains hidden or 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
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