Before ES2015, every classic script shared one global scope, so any top-level var or function could collide with another file's. The module pattern fixed that with a closure: wrap the file in an IIFE, keep state and helpers as locals of that call, and return an object whose methods close over them. Nothing outside can reach the locals; the returned object is the whole API. Dependencies came in as IIFE arguments, such as (function ($) { … })(jQuery).
The revealing module variant writes every function as a private local and ends with return { add, increment }, so the API is listed in one place.
ES modules took over each piece:
- Scope. Every module gets its own Module Environment Record. Top-level declarations are not global properties, so no wrapper is needed.
- API.
exportreplaces the returned object. An import is an immutable indirect binding (CreateImportBinding): importers see the exporter's current value and can't assign to it. - Run once. A host gives back the same Module Record for the same request, and the record is evaluated once. In practice that means once per resolved URL per realm (browser window or worker, Node.js process), which is the IIFE's "single instance" behaviour.
- Dependencies. Static
importdeclarations replace IIFE parameters, so tools can see the graph before running it. - Strictness. Module code is always strict.
The IIFE pattern survives where there are no modules: classic <script> tags, and bundler output (esbuild's iife format exists to keep bundle variables out of the global scope). If you need many instances, you want a factory function, not a module.