Revealing Module Pattern in JavaScript

The previous articles explained the Prototype Pattern. You can read them at the following links:

  1. Understanding Prototype Pattern in JavaScript
  2. Understanding Prototype Pattern in JavaScript: Part # 2

The Revealing Module Pattern allows us to create:

  1. Singleton object
  2. Private Public behavior of functions and properties in the object.

Let us consider the usual code we write. We also know it as functional spaghetti code.

var basicSalary;
        var salary;
        function CalculateSalaray(basicSalary) {
            salary = basicSalary * 10;
        }
        function PrintSalaray() {
            alert("Salary is " + salary);
        }

        CalculateSalaray(200);
        PrintSalaray();


There is nothing wrong in the code snippet above. However:

  • It is hard to maintain when functionality grows
  • It is hard to debug
  • It is hard to test

In the Revealing Module Pattern all code is placed inside a function represented by a variable. This is a self-executed function.

Revealing-Module-Pattern-in -JavaScript

The Revealing Module Pattern allows us to:

  1. Create public private members
  2. Make code more readable

In this pattern we put all functions and variables inside the object. In the above case everything would be encapsulated inside a salary object. To make anything public we need to make them part of a return statement.

  var salaray = function () {

            //Private
            var sal;               
            function calculateSal(basesal) {
                sal = basesal*1000;
            }

            function printSal(basesal) {
                calculateSal(basesal);
                console.log(sal);
               
            }
         
            //Public 

            return {
               PrintSalaryprintSal
                
            };
        }();

        salaray.PrintSalary(2000);

In the snippet code above we have two private functions. One private function is exposed as public in the return statement. Since the object is wrapped as a self-executable function hence it works as a singleton. We can call public functions on the object itself.

If we want to remove a singleton then the following two steps need to be done:

  1. Remove self-executable code

  2. Create a new object using the new operator

So the object salary above can be modified as in the following. Now it will have a private public property but it would not act as a singleton.
 

var salaray = function () {

            //Private
            var sal;               
            function calculateSal(basesal) {
                sal = basesal*1000;
            }

            function printSal(basesal) {
                calculateSal(basesal);
                console.log(sal);
               
            }
         
            //Public 

            return {
               PrintSalaryprintSal
                
            };

        };

        var mysal = new salaray();
        mysal.PrintSalary(789);
        var mysal1 = new salaray();
        mysal.PrintSalary(100);


In the code above the salary object is not a singleton and has private and public functions. In many terms the Revealing Module Pattern can be considered as one of the best JavaScript patterns. Three advantages of the Revealing Module Pattern are as follows:

  1. Creates a Singleton

  2. Gets Private or Public method behavior.

  3. Better readability of code

I hope you find this article useful. Thanks for reading.
 

Previous article: Understanding Prototype Pattern in JavaScript: Part # 2

Up Next
    Ebook Download
    View all
    Learn
    View all