Skip to content

Instantly share code, notes, and snippets.

@jonkemp
Created March 29, 2021 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonkemp/59c0e659f6f94676d7e3575019617162 to your computer and use it in GitHub Desktop.
Save jonkemp/59c0e659f6f94676d7e3575019617162 to your computer and use it in GitHub Desktop.
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