Design Patterns

Revealing Module Pattern

A refinement of the Module Pattern where all logic is defined privately and a single, clean return statement reveals exactly what is public. Your API surface becomes immediately visible, self-documenting, and consistent.

What is the revealing module pattern?

The revealing module pattern is a variation of the module pattern where all functions are defined privately inside the closure, and the returned object simply maps public names to them: return { add, remove }. It makes the public API easy to see at a glance and lets private functions call each other by name.

const cart = (() => {
  const items = [];
  function add(item) { items.push(item); }
  function total() { return items.reduce((s, i) => s + i.price, 0); }
  return { add, total };      // "reveal" the public API
})();

Module Pattern vs Revealing Module

FeatureModule PatternRevealing Module Pattern
Where functions definedInline in return objectAll private, revealed by name in return
API surface visibilityScattered throughout returnSingle clean return statement
Can rename public APIAwkwardClean: return { pub: privateFunc }
ConsistencyMixed styleUniform — everything private first
Override public methodsPossible (risk)Possible but doesn't affect private calls
Debugging easeModerateHigh — all logic in named functions

Real-World Use Cases

Event Bus

on/off/emit/once — event-driven communication between modules

Config Module

Environment-aware settings with private defaults and public getter

Shopping Cart

Items array, discount, totals — all private, clean add/remove API

Logger

Private log levels/formatters, public log/warn/error/group methods

State Container

Private state, public get/set/subscribe — observable data store

Router

Private routes registry, public navigate/on/back/getPath methods

Common Pitfalls

Overriding a revealed method from outside — private functions still call the original
If Module.method is replaced externally, internal private calls still use the original. This is a feature: internals are immune to outside overrides.
Exposing private helpers that should stay internal — leaks implementation details
Be strict about the return object. Only expose what callers genuinely need. Private helpers stay private.
Circular references in private closures — memory leaks in long-running apps
Add a destroy() method: nulls out closured arrays/objects. Call it when the module is no longer needed.
Creating many instances of a Revealing Module for per-instance state
IIFE creates a singleton. For multiple instances, use a factory function or class instead.

Interactive Demo

Code Editor

<button id="add">Add $10 item</button>
<button id="clear">Clear</button>
<p id="out"></p>

Preview

Revealing Module Pattern: summary

Key points

  • The public API is listed in one place at the bottom.
  • Overriding a revealed method from outside doesn't change internal calls, which can surprise you.
  • ES module exports serve the same purpose in modern code.

Frequently asked questions

›What is the difference from the module pattern?

In the basic module pattern, methods are written directly inside the returned object. In the revealing variant, everything is defined privately first, and the return statement only exposes references.