Skip to content

Instantly share code, notes, and snippets.

@jacksoncharles
Last active December 22, 2015 05:38
Show Gist options
  • Save jacksoncharles/6424851 to your computer and use it in GitHub Desktop.
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
<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