Module Pattern

What is known as a "module" in software development represents some collection of components and features that can be put together to perform some useful work. In JavaScript, the module pattern is one of the most used patterns for many reasons. For most, it helps to enforce clean code, eliminates constant declarations (this and prototype), and is easy to grasp and apply.

The module pattern depends on what is known as immediately-invoked function expression, or shortly IIFE, which is a well-known idiom in the JavaScript world. The main idea of the IIFE is to retain privacy for the variables and the functions inside of it and prevent polluting the global environment (global variables are a source of bugs and confusion and should be avoided). IIFE looks like the following.

(function() {

    // ...

})();

The function is defined and executed instantly. Let's see how the module pattern could be defined in general using the IIFE idiom.

var module = (function() {

    // ...

})();

By setting the module in the global scope, it can be accessible from anywhere in the rest of the code, yet its scope is private. This is the good part of the module pattern. We can define methods/functions inside of the module like so.

var module = (function() {

    var method = function () {
        // method
    }

})();

That method is not accessible outside of the module. Therefore, the privacy has been applied. How could it be beneficial in this case if there is nothing could be accessed then? By exposing what is meant to be for the public use as an API to other developers using an object literal for instance.

var module = (function() {

    var privateMethod = function () {
        // private method
    }

    return {
        publicMethod: function () {
            // public method that could use the private method 
        }
    }
})();

Revealing Module Pattern

The previous bit is close to the revealing module because it reveals some or all of the module functions. To achieve the full revealing module, all the methods should be defined inside the scope of the module and expose only those methods/variables that need to be revealed by setting public pointers to them.


var Module = (function () {

    var privateMethod = function () {
        // private method
    };

    var publicMethod = function () {
        // public method
    };

    var publicMethodTwo = function () {
        // public method two
    };
  
    return {
        exposedMethod: publicMethod,
        exposedMethodTwo: publicMethodTwo
    };

})();

Again, the privateMethod is not accessible outside of the module scope, but it could be used inside of publicMethod and publicMethodTwo which are exposed as an API that the module provide.

One of the use cases where creating a private method is useful is when making calls to a back-end API. The logic of handling the request and returning a ready-to-use set of data does not need to be "revealed", but it is useful for the rest of the public methods.