Last active
December 22, 2015 05:38
-
-
Save jacksoncharles/6424851 to your computer and use it in GitHub Desktop.
Simple example courtesy of Steven Robinson. You can just return a seperate object containing public mehods, but this way is better as then all functions are accessible within the outer function
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
<script type="text/javascript"> | |
var COUNTER = (function () { | |
var counter = {}, | |
num = 0, | |
increment = function () { | |
console.log(num += 1); | |
}, | |
reset = function () { | |
num = 0; | |
}; | |
counter.inc = increment; | |
counter.reset = reset; | |
return counter; | |
}()); | |
COUNTER.inc(); | |
COUNTER.inc(); | |
COUNTER.inc(); | |
COUNTER.reset(); | |
COUNTER.inc(); | |
COUNTER.inc(); | |
COUNTER.inc(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment