Skip to content

Instantly share code, notes, and snippets.

@rohit-lakhanpal
Created April 19, 2016 04:43
Show Gist options
  • Save rohit-lakhanpal/ef97f1aa360402cbb1194c097cc84630 to your computer and use it in GitHub Desktop.
Save rohit-lakhanpal/ef97f1aa360402cbb1194c097cc84630 to your computer and use it in GitHub Desktop.
JS-Module Pattern: Module pattern is used to further emulate the concept of classes in such a way that we're able to include both public/private methods and variables inside a single object, thus shielding particular parts from the global scope. What this results in is a reduction in the likelihood of our function names conflicting with other fu…
var testModule = (function () {
var counter = 0;
return {
incrementCounter: function () {
return counter++;
},
resetCounter: function () {
console.log( "counter value prior to reset: " + counter );
counter = 0;
}
};
})();
// Usage:
// Increment our counter
testModule.incrementCounter();
// Check the counter value and reset
// Outputs: counter value prior to reset: 1
testModule.resetCounter();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment