Skip to content

Module Pattern

The module pattern uses an IIFE to create a closure that encapsulates private data and exposes a public API.

const counterModule = (function () {
let count = 0;
return {
increment() {
count++;
},
decrement() {
count--;
},
getCount() {
return count;
},
};
})();
counterModule.increment();
console.log(counterModule.getCount()); // 1