Created
October 9, 2013 10:42
-
-
Save cbednarski/6899351 to your computer and use it in GitHub Desktop.
Example of private data in a closure in javascript
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
| var lib = (function (win) { | |
| var sum = 0; | |
| return { | |
| makeSum: function (arr) { | |
| win.document.body; | |
| JSoop.each(arr, function (value, index) { | |
| sum = sum + value; | |
| }); | |
| return sum; | |
| } | |
| }; | |
| }(window)); | |
| lib.makeSum([1, 2, 3]); //6 | |
| lib.makeSum([1, 2, 3]); //12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment