Created
August 8, 2014 22:13
-
-
Save gatlin/ef05b1dc708343fd06ee to your computer and use it in GitHub Desktop.
Example of using closures in JavaScript to create a protected generator
This file contains 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 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