- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Module Patterns
JavaScript Modules
JavaScript Module Patterns
Before real modules existed, JavaScript used closures to fake the same isolation.
This is worth knowing because it still appears throughout older code.
It is also a good way to see exactly what real modules give you for free.
Example
Example
javascript
const counterModule = (function () {
let count = 0;
return {
increment: function () { count++; return count; },
reset: function () { count = 0; }
};
})();
console.log(counterModule.increment());
console.log(counterModule.increment());