Skip to content

Instantly share code, notes, and snippets.

@raypulver
Created June 9, 2016 20:06
Show Gist options
  • Save raypulver/f3af8ed70522a748c89fd2cba227bed0 to your computer and use it in GitHub Desktop.
Save raypulver/f3af8ed70522a748c89fd2cba227bed0 to your computer and use it in GitHub Desktop.
function static storage example
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