The original module pattern is a great way to structure and organize JavaScript code. The pattern provides a tool for writing self-contained decoupled pieces of code, enforcing privacy to everything within the module that is not explicitly revealed. The module pattern is a combination of other patterns, some of which will be briefly touched on before we end up with the revealing module pattern; a variant of the original module pattern.
The revealing module pattern:
my.module = (function () {
var iAmAPrivateVariable,
iAmAPrivateMethod = function () {
// do awesome stuff
},
iAmAPrivateMethodNeverRevealed = function () {
// do really awesome stuff
};
return {
iAmAPublicVariable : iAmAPrivateVariable,
iAmAPublicMethod : iAmAPrivateMethod
};
}());
Now, let’s break this down piece by piece.
First off, we need a namespace. It was omitted above for clarity.
var my = window.my || {};
Each module is attached to the namespace, which is the ONLY global variable introduced. Global variables are EVIL!
We then add a property to the namespace and reference a function expression. In this example the name of the module is module, but it can be just about anything.
my.module = (function () {
console.log( "I'm alive and kicking!" )
}());
Note the parenthesis surrounding the function expression and the pair of parenthesis directly following the closing gullwing. This pattern turns the function expression into an immediate function, also known as a self-executing function, and enables us to execute the function as soon as it is defined.
JavaScript is function scoped, that is, everything declared within a function is private to that function and does not pollute the global namespace.
my.module = (function () {
var iAmAPrivateVariable,
iAmAPrivateMethod = function () {
// do awesome stuff
};
}());
To expose part of our module, we declare the public variables and methods in the return clause. This is the original module pattern.
my.module = (function () {
var iAmAPrivateVariable,
iAmAPrivateMethod = function () {
// do awesome stuff
};
return {
iAmAPublicVariable : "",
iAmAPublicMethod : function () {
// do even more awesome stuff
}
};
}());
When executed (immediately, that is) the my.module property is populated with the iAmAPublicVariable variable and the iAmAPublicMethod method. Everything else is still private and tucked away from the rest of the world.
Combining the original module pattern (above) with the revelation pattern, which is all about having private methods and exposing them as public methods, we end up with the revealing module pattern. This pattern allows us to declare everything in the module as private and then explicitly reveal the private API in the return clause by referencing private variables and methods.
my.module = (function () {
var iAmAPrivateVariable,
iAmAPrivateMethod = function () {
// do awesome stuff
},
iAmAPrivateMethodNeverRevealed = function () {
// do really awesome stuff
};
return {
iAmAPublicVariable : iAmAPrivateVariable,
iAmAPublicMethod : iAmAPrivateMethod
};
}());
That’s it for now. Happy coding!