Created
April 19, 2016 04:43
-
-
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…
This file contains 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 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