Facebook Pixel

Closures and the Module Pattern in JavaScript

The module pattern uses closures to create public APIs with private state. Here is how it works.

Closures and the Module Pattern in JavaScript

The module pattern uses closures to create a public API with private state. It was the standard way to organize code before ES6 modules.

The Classic Module Pattern

const myModule = (function () { const privateVar = "secret"; function privateMethod() { return privateVar.toUpperCase(); } return { publicMethod: function () { return privateMethod(); }, publicVar: "public", }; })(); myModule.publicMethod(); // "SECRET" myModule.publicVar; // "public" myModule.privateVar; // undefined (private) myModule.privateMethod; // undefined (private) `` An IIFE creates a private scope. Inside, `privateVar` and `privateMethod` are local. The returned object's methods close over them. Only the returned object is accessible from outside. ### The Revealing Module Pattern A variation where you define all functions as private, then return an object that reveals which ones are public: ```js const myModule = (function () { let count = 0; function increment() { return ++count; } function decrement() { return --count; } function getCount() { return count; } return { increment, decrement, getCount, }; })(); `` This is cleaner: all functions are defined as private, and the return object maps public names to private functions. ### ES6 Modules (Modern Alternative) ```js // counter.js let count = 0; export function increment() { return ++count; } export function decrement() { return --count; } export function getCount() { return count; } // count is module-scoped (private to the module) `` ES6 modules provide module scope automatically. Top-level variables are private to the module unless exported. This replaces the IIFE module pattern. ### Advantages of the Module Pattern - **Encapsulation**: private state is hidden. - **Public API**: only the returned methods are accessible. - **No global pollution**: everything is inside the IIFE. - **State persistence**: the closed-over variables persist across calls. ### When to Use Each - **ES6 modules**: for all modern code. Use `export`/`import`. - **Module pattern (IIFE)**: in old codebases without module support, or in browser scripts without a bundler. - **`#private` class fields**: for class-based encapsulation. ### The Takeaway The module pattern uses an IIFE to create private scope and returns a public API. The returned methods close over private variables. The revealing module pattern is a cleaner variation. ES6 modules are the modern replacement, providing module scope automatically. Use ES6 modules for new code.

An IIFE that creates a private scope and returns an object with public methods. Private variables and functions are hidden inside the closure. Only the returned object is accessible from outside.

A variation where all functions are defined as private, and the return object maps public names to private functions. This is cleaner because all implementations are defined in one place and the return object just reveals which are public.

ES6 modules provide module scope automatically. Top-level variables are private to the module unless exported. This replaces the IIFE module pattern with a cleaner syntax using export and import.

Encapsulation (private state is hidden), a clear public API (only returned methods are accessible), no global pollution (everything is inside the IIFE), and state persistence (closed-over variables persist across calls).

Use ES6 modules for all modern code (export/import). Use the IIFE module pattern only in old codebases without module support, or in browser scripts without a bundler. Use #private class fields for class-based encapsulation.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.