What is the module pattern and its evolution?
Answer
The module pattern encapsulates private state and exposes a public API. It evolved through several stages. Classic IIFE Module (pre-ES6): const counter = (function() { let count = 0; return { inc() { return ++count; }, reset() { count = 0; } }; })() — private count, public interface. Revealing Module: all logic defined privately, then selectively exposed — cleaner but can be confusing about what is public. CommonJS (Node.js): module.exports = { inc, reset } — synchronous, each file is a module with private scope. AMD (Asynchronous Module Definition): define(["dep"], function(dep) { return { } }) — async loading for browsers (RequireJS). UMD (Universal Module Definition): works in both CommonJS and AMD environments — used for libraries. ES Modules (ES6, current standard): export const inc = () => ++count; export default counter — static, tree-shakeable, async-loadable, native browser support. ES Modules are the definitive standard — all other patterns are legacy. Understanding the evolution helps when reading older codebases that use IIFE or CommonJS patterns.
Previous
What is the difference between inheritance patterns in JavaScript?
Next
What is the concept of pure functions and side effects?