Alexander Dickson

JavaScript's Revealing Module Pattern

The Revealing Module Pattern in JavaScript is a pattern that gives you information hiding in JavaScript, a language in which explicit visibility modifiers are non-existent.

The pattern was developed by Richard Cornford et al on comp.lang.javascript1.

The pattern

Let’s have a geezer at it…

var module = (function() {

    var private = function() {
        // ...

    var public = function() {
        // ...
    return {
        public: public


typeof module.public; // "function"
typeof module.private; // "undefined"

Why is this a Good Thing?

As mentioned in the introduction, JavaScript’s object implementation is unlike many other languages in which you can’t define properties as public, protected or private. By default, all properties are public. Some people use the _ prefix for private methods, but it’s only cue to other developers; nothing is stopping you from calling that method.

To achieve something similar in JavaScript, we need to rely on closures to get the ability to hide methods and properties. That’s why we see the Revealing Module Pattern championed to fix this shortcoming.

Want to discuss this post? Just mention me @alexdickson.