ReviseAlgo Logo

Design Patterns

Module Pattern & Revealing Module Pattern

Master encapsulation in JavaScript. Learn the Module Pattern and Revealing Module Pattern using closures and IIFEs to hide private state.

Last Updated: July 15, 2026 10 min read

1. Introduction

Before ES Modules were introduced, JavaScript lacked native module systems. Developers used the Module Pattern and Revealing Module Pattern (leveraging IIFEs and function closures) to isolate scopes and create private properties.

2. Why It Matters

Encapsulation is a core concept in software engineering. The Module Pattern allows you to group public methods and private variables inside a single namespace container, preventing global scope contamination and protecting internal state.

3. Real-World Analogy

Think of a Bank Safety Deposit Desk:

  • Standard Object (Open Counter): Your ledger is placed on a desk in the lobby. Anyone walking by can write on the ledger or change numbers directly.
  • Module Pattern (Private Teller Desk): A teller sits behind a security window. The teller maintains a private ledger (private state) in their desk. The teller exposes specific slot slots on the window counter: "Request Balance" or "Deposit Money" (public methods). You cannot reach through the window to grab the ledger; you must ask the teller to update it for you, protecting the records.

4. The Module Pattern

The standard Module Pattern uses an IIFE to return an object containing public methods that can access private variables via closures:

5. The Revealing Module Pattern

The Revealing Module Pattern is a variation where you define all variables and functions in the private scope, and return an object containing references (pointers) to the private functions you want to make public:

6. Practical Example

This script demonstrates creating a secure counter module using the Revealing Module Pattern:

7. Common Mistakes

  • Overwriting public pointers in the Revealing Module Pattern: If a public pointer is reassigned from outside (e.g. UserModule.get = customFn), it only breaks the public property reference. Sibling public methods still call the original private function, which can lead to inconsistent behavior.

8. Quick Quiz

Q1: What mechanism does the Module Pattern rely on to hide private variables?

A) Prototypal inheritance chains

B) Function closures combined with an IIFE

Answer: B — Function closures allow the returned public methods to access variables declared inside the IIFE wrapper, keeping them private.

9. Scenario-Based Challenge

The Encapsulated Config Manager:

You want to write a configuration manager module. The module has private configuration settings: { port: 8080 }. Write a Revealing Module Pattern wrapper that exposes a getSetting(key) method, preventing developers from mutating settings directly.

10. Debugging Exercise

Explain why this public method fails to return the updated private variable, and how to fix it:

const BadModule = (function() {
  let count = 0;

// Returning the primitive value directly inside the public object! return { count, // Copies the value 0! increment() { count++; // Mutates private variable count, but public object has copy! } }; })();

BadModule.increment(); console.log(BadModule.count); // logs 0 instead of 1! Why?

View Solution

Diagnosis: The module returns a primitive number value count inside the public object. Since primitive values are copied, mutating the private variable count does not update the copied value on the public object.

Fix: Expose a public getter method (or function pointer) to read the private variable's value dynamically:

const GoodModule = (function() {
  let count = 0;

return { getCount() { return count; }, // Accesses private variable via closure increment() { count++; } }; })();

11. Interview Questions

🟢 Q1: Compare the standard Module Pattern and the Revealing Module Pattern.

Answer:
Module Pattern: Public methods are defined directly inside the returned object (e.g. return { method() { ... } }).
Revealing Module Pattern: All variables and functions are defined in the private scope first. The returned object simply contains pointers to those private functions (e.g. return { publicMethod: privateMethod }).
The Revealing Module Pattern makes code easier to read by keeping all implementation logic in the private scope, but reassigning a public pointer from outside can cause inconsistent behavior.

12. Production Considerations

  • Transition to ES Modules: In modern production code, prefer using ES Modules (using import/export) instead of writing IIFE-based modules. ES Modules are standard, support static analysis, and handle scope isolation natively.