Bob Nadler, Jr. Bob Nadler, Jr.

JavaScript Patterns: Revealing Module

Published about 13 years ago less than 1 min read

Awhile ago I wrote about the Module Pattern in JavaScript. There is an alternative that is quite popular called the Revealing Module Pattern that looks like this:

The problem is, I don't like it. My main issue with this pattern is readability: it makes it easy to fall into the trap of confusing which functions are private vs. public. This is especially true when you come across code that alternates private and public functions haphazardly like this:

Yes, I know that you can just scroll down to the "return" statement to see which functions are public, but I find that having the public function definitions in the "return" block more intuitive when reading code. The whole point of private functions is to provide abstraction.

When using the regular Module Pattern I can read the public function and scroll up to the private definitions if needed. When reading the module from the top I don't need to keep a mental model to track which functions are public vs. private.


Share This Article