Last active
August 29, 2015 14:26
-
-
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.
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
| 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