Javascript Revealing Module Pattern

On a previous post  I refer the javascript Revealing Module Pattern, in this post I want to explain how it works and how can it help you to structure your javascript code. 

First lets look to its structure:

var Calculator = function() {
  // code goes here
}();

The code starts with a definition of a variable, which we associate a function that is invoked as soon as as the script loads (this is done with the “()” at the end).

Your variables and functions should go inside of the Calculator in the “code goes here” part.

Now, one of the most interesting parts, inside the Calculator we can declare private and public members. For the private members we just have to declare them inside of the Calculator and, for the public members, we have to add a return statement and declare the public members inside it.

Lets look at the structure of the complete Revealing Module Pattern:

var calculator = function() {
  // private members goes here
  // ...
  // public members goes here
  return{
    // ...
  };
}();

And now lets look at the implementation of the Calculator that just adds and subtracts 2 numbers and logs the result to the console if configured that way:

var Calculator = function() {
  // private members goes here
  var privateLog = false;
  var privateConfig = function(shouldLog) {
    privateLog = shouldLog;
  };
  var privateAdd = function (x,y) {
    var result = x + y;
    if (privateLog) {
      console.log(result);
    }
    return result;
  };
  var privateSubtract = function (x,y) {
    var result = x - y;
    if (privateLog) {
      console.log(result);
    }
    return result;
  };
  // public members goes here
  return{
    publicConfig: privateConfig,
    publicAdd: privateAdd,
    publicSubtract: privateSubtract
  };
}();

Calculator.publicConfig(true); // configures Calculator to log to console
Calculator.publicAdd(1,2); // logs 3
Calculator.publicSubtract(2,1); // logs 1

Analyzing the code for the Calculator, you can see that all public members are exposed in the object literal that is returned when the Calculator is invoked. All the other members are private and cannot be accessed outside of the Calculator.

The Revealing Module Pattern is one of my favorites design patterns to structure my javascript code because it is easy to implement, easy to read (which is good for maintenance) and for the capability to declare private and public members.

You can see this code in this jsFiddle.

Advertisements

One thought on “Javascript Revealing Module Pattern

  1. Pingback: Get into RequireJS | Nuno Oliveira

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 )

Google+ photo

You are commenting using your Google+ 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