Created
March 29, 2021 17:55
-
-
Save jonkemp/59c0e659f6f94676d7e3575019617162 to your computer and use it in GitHub Desktop.
The Module Pattern from Learning JavaScript Design Patterns by Addy Osmani
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
const testModule = (() => { | |
let counter = 0; | |
return { | |
incrementCounter() { | |
return counter++; | |
}, | |
resetCounter() { | |
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