Coding Guru

Code never does the mistake, we make the mistakes in the code. Think, Imagine, Phrase and then Code.

The Revealing Module Pattern

The revealing module pattern is nothing much more then an extension to the module pattern which focuses on the public and private methods by invoking the concept of encapsulation. The only separation between both of these is the way to verify that all the methods and variables are kept private until unless they are exposed explicitly by an object or by the closure its being defined.

Now when are already familiar with the concept of module pattern and the modules, it would be easy for us to understand why this revealing word is being used. In this pattern we reveal the public pointers (variables) to the methods inside a module's scope. And doing so we achieve an easy code management system. Lets have a look at an exemple :

var MyModule = (function () {

  var myPrivateMethod = function () {
    console.log("Private method is called")

  var myPublicMethod = function () {
    console.log("Public method is called");
myPrivateMethod ()
 }; var anotherPublicMethod = function () {
console.log("Another public method is called"); }; return { myPublicMethod : myPublicMethod , anotherPublicMethod : anotherPublicMethod }; })();

So as per the above code stub its enough descriptive to let us know how only two methods are being exposed when an object for this module is created. Although the private method i.e myPrivateMethod() is a part of public method myPublicMethod but is unavailable for explicit use


  • Cleaner approach for developers
  • Code is much manageable and readable
  • Supports private methods and data as well
  • Variables and functions are localized by using closures
  • Variables and functions can be encapsulated
  • Implicit exposure of public methods by using return statement


  • Private methods are inaccessible : Some developers say that when we do so the private functions are not exposed and on some point when we realize to use it we have to update the return section. But the fact is this is what we actually do in object oriented programming.
  • Private methods can't be unit tested : Many developers complain about inability for unit testing but not everyone. When we automate the testing by using QUnit or Jasmine or some other testing framework we always have to make the methods public.

Pingbacks and trackbacks (1)+