Skip to content

Instantly share code, notes, and snippets.

@dnasca
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save dnasca/2e9674761848fc852575 to your computer and use it in GitHub Desktop.

Select an option

Save dnasca/2e9674761848fc852575 to your computer and use it in GitHub Desktop.
A super simple example of a function closure in JavaScript. The example shows how to increment a private property within a function closure.
result = (function () {
var result = 0;
return {
value: function() {
return value;
},
increment: function() {
return ++value;
}
};
}());
//call result.value() //output: value = 0
//call result.increment();
//call result.value() //output: value = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment