Skip to content

Instantly share code, notes, and snippets.

@gatlin
Created August 8, 2014 22:13
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 gatlin/ef05b1dc708343fd06ee to your computer and use it in GitHub Desktop.
Save gatlin/ef05b1dc708343fd06ee to your computer and use it in GitHub Desktop.
Example of using closures in JavaScript to create a protected generator
var getId = function() {
var _id = 0;
return function() {
return _id++;
}
}()
// usage
console.log(getId());
console.log(getId());
console.log(getId());
// Prints:
// 0
// 1
// 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment