The Revealing Module Pattern in JavaScript

The Revealing Module Pattern is a design pattern in JavaScript that organizes code by encapsulating private implementation details and explicitly exposing a clean public API. By utilizing Immediately Invoked Function Expressions (IIFEs) and closures, this pattern allows developers to define all functions and variables within a private scope and return an anonymous object with pointers to only the methods and properties intended for external use.

How the Pattern Works

The pattern relies on JavaScript closures to maintain state and manage variable scope. The implementation follows a straightforward three-part structure:

  1. The IIFE Wrapper: An Immediately Invoked Function Expression wraps the entire module, creating a distinct local scope that prevents variables from polluting the global namespace.
  2. Private Definitions: All variables and functions are declared locally within the module using standard function and variable declarations. By default, everything remains private.
  3. Explicit Public Export: At the bottom of the module, an object literal is returned. This object maps specific internal functions and variables to public keys, clearly defining the module’s public interface.
const UserModule = (function () {
    // Private properties and methods
    let userCount = 0;

    function logUserCreation(name) {
        console.log(`User created: ${name}`);
    }

    function createUser(name) {
        userCount++;
        logUserCreation(name);
        return { id: userCount, name: name };
    }

    function getUserCount() {
        return userCount;
    }

    // Reveal public pointers
    return {
        create: createUser,
        count: getUserCount
    };
})();

// Usage
const newUser = UserModule.create("Alice"); // Output: User created: Alice
console.log(UserModule.count()); // Output: 1
console.log(UserModule.userCount); // Output: undefined (remains private)

Structural Advantages

Trade-offs to Consider