Skip to content

Instantly share code, notes, and snippets.

@arnab
Created September 13, 2011 04:47
Show Gist options
  • Select an option

  • Save arnab/1213133 to your computer and use it in GitHub Desktop.

Select an option

Save arnab/1213133 to your computer and use it in GitHub Desktop.
# 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
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