Created
September 13, 2011 04:47
-
-
Save arnab/1213133 to your computer and use it in GitHub Desktop.
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
| # From http://nathansjslessons.appspot.com/lesson?id=1080 | |
| counter = ( -> | |
| count = 0 # cannot be accessed as it's tied to the closure | |
| f = -> | |
| count += 1 | |
| f | |
| )() | |
| console.log counter() # 1 | |
| console.log counter() # 2 | |
| console.log counter() # 3 | |
| console.log counter() # 4 |
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
| var counter; | |
| counter = (function() { | |
| var count, f; | |
| count = 0; | |
| f = function() { | |
| return count += 1; | |
| }; | |
| return f; | |
| })(); | |
| console.log(counter()); | |
| console.log(counter()); | |
| console.log(counter()); | |
| console.log(counter()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment