Skip to content

Instantly share code, notes, and snippets.

@jonkemp
Created Mar 29, 2021
Embed
What would you like to do?
The Module Pattern from Learning JavaScript Design Patterns by Addy Osmani
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