Revealing Module Pattern in JavaScript

We talked about Prototype Pattern in previous posts. You can read them at below link.

Understanding Prototype Pattern in JavaScript

Understanding Prototype Pattern in JavaScript: Part # 2

Revealing Module Pattern allows us to achieve

  • Singleton object
  • Private Public behaviour of functions and properties in the object.

Let us consider the usual code we write. We also known as that 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 above code snippet. However,

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

In Revealing Module pattern we put all code inside a function represented by a variable. This is self-executed function.

image

Revealing module pattern allows us

  • To achieve public private members
  • To make code more readable

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


var salaray = function () {

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

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

 }

 //Public

return {
 PrintSalary: printSal

 };

}();

salaray.PrintSalary(2000);

 

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

If we want to remove singleton then two steps need to be followed,

  • Remove self-executable code
  • Create new object using new operator.

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


var salaray = function () {

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

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

 }

 //Public

return {
 PrintSalary: printSal

 };

};

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

 

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

  1. Achieve Singleton
  2. Get Private –Public method behaviour
  3. Better readability of code

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

4 responses to “Revealing Module Pattern in JavaScript”

  1. […] Revealing Module Pattern in JavaScript (Dhananjay Kumar) […]

  2. If you’re a fan of the Module Pattern, you might also try the Definitive Module Pattern:
    https://github.com/tfmontague/definitive-module-pattern

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com