Created
June 9, 2016 20:06
-
-
Save raypulver/f3af8ed70522a748c89fd2cba227bed0 to your computer and use it in GitHub Desktop.
function static storage example
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 myFunc() { | |
if (!myFunc.static) myFunc.static = Object.create(null); | |
// now we can access this hash every time we call the function, export it along with the function, and it is purely associated with the function | |
if (!myFunc.static.x) myFunc.static.x = 0; | |
myFunc.static.x++; | |
return myFunc.static.x; | |
} | |
myFunc(); | |
myFunc(); | |
myFunc(); | |
console.log(myFunc()); | |
// 4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment