Sitemap
Ryo Osawa

Ryo Osawa

Software Engineer. Live in San Francisco. Coffee addict.

Javascript Module Pattern

--

Module Pattern is one of the common design patterns in Javascript and it’s used in many popular Javascript libraries. This design pattern can assist in encapsulating and organizing your code by hiding all the heavy lifting details and private states and only exposing the methods that other parts of your application use. I like the Module Pattern because it is very easy to implement and being able to define a class-like object that provides both private and public methods is a familiar programming concept that I’m very much used to.

Get Ryo Osawa’s stories in your inbox

Join Medium for free to get updates from this writer.

Because Javascript does not offer access modifiers as part of its native language feature unlike some other programming languages like Java, C#, we need to use some techniques/features to create a module that works like a class in Javascript. Here are some key constructs that you need to use in order to define a module in Javascript:

  • Immediately-invoked functional expression
  • Object literal as a return value
  • Closure

Define a Module

To define a module, the first thing you need to is use an immediately-invoked functional expression like below.

// myModule.js — define a namespace for myModule
var myModule = (function() {
// your code is defined here
}());

The above code declares a simple skeleton module called myModule. Because Javascript supports the lexical scope, all variables and methods declared inside of this module will not be accessible from outside. Also, you can define a namespace by assigning the execution result of this expression to a variable so that you can reference it in your code — most likely in other Javascript files.

Private Methods

To expose your public methods or services, you need to define an object and use it as a return value. For example, if you want to expose two public methods in your module, you can do as follows:

var myModule = (function() {  // your code is defined here return {
// Public methods are here
publicMethod1: function() { … },
publicMethod2: function() }{ … }
};
}());

Using the above example, if you want to invoke publicMethod1 somewhere in your application code, you simply invoke like below.

//app.js
myModule.publicMethod1();

Private Members and Methods

For private members and methods, all you need to do is define them inside of your module function.

var myModule = (function() { // Private members
privateMember1 = 0;
privateMember2 = [“bar”, “baz”];

// Private methods
privateMethod1 = function() { … };
privateMethod2 = function() { … };
return {
// Public methods are here
publicMethod1: function() { … },
publicMethod2: function() }{ … }
};
}());

Because both public and private members/methods are declared in the same scope, public members can access to private members and methods. This way, you can invoke some internal private methods to carry out your public methods, which helps your code modularized and organized.

var myModule = (function() {
// Private members
privateMember1 = 0;
privateMember2 = [“bar”, “baz”];
// Private methods
privateMethod1 = function() { … };
privateMethod2 = function() { … };
return {
// Public methods are here
publicMethod1: function() {
// Invoke private method
privateMember1++;
return privateMetehod1(privateMember);
},
publicMethod2: function() }{ … }
};
}());

One potential drawback of using the Module Pattern would be that you cannot write unit tests for your private methods as there is no way to access these methods outside of your module. However, it is often claimed that because these are private/internal methods, these public methods should not be unit-tested. Rather, you should focus on writing unit tests for your public methods.

Advanced Techniques & Module Pattern Variations

Finally, because Module Pattern is very popular, there are some advanced techniques that you can use to utilize this design pattern. For example, if you want to import some other modules or reference Javascript libraries like jQuery or underscore inside your module, you can pass in those as an argument to your module like below.

var myModule = (function() {
// Private members
privateMember1 = 0;
privateMember2 = [“bar”, “baz”];
// Private methods
privateMethod1 = function() {
var $p = $(‘p’); // Select all p nodes using jQuery
_.each($p, function(p) {
// Iterate each p element using underscore’s each method
});
};
privateMethod2 = function() { … };
return {
// Public methods are here
publicMethod1: function() {
// invoke private method
privateMember1++;
return privateMetehod1(privateMember);
},
publicMethod2: function() }{ … }
};
}(jQuery, _)); // Import jQuery and underscore libraries

Also, there is a variation of this pattern called Revealing Module Pattern, which essentially defines all public methods outside of your return object and include their references as a property. This way, the implementations of your public methods do not need to be there inside of your return object. Some people like this approach because it keeps your function code away from the return object.

var myModule = (function() {
// Private members
privateMember1 = 0;
privateMember2 = [“bar”, “baz”];
// Private methods
privateMethod1 = function() { … };
privateMethod2 = function() { … };
// Public methods are defined here
publicMethod1: function() }{ … };
publicMethod2: function() }{ … };
return {
// Public methods are exposed here
publicMethod1: publicMethod1,
publicMethod2: publicMethod2
};
}());

Hope this post helps you understand the Javascript Module Pattern and how to define it. If you’re interested in learning about advanced techniques using Module Pattern and other design patterns in Javascript, you should check out this amazing site.

--

--

Ryo Osawa
Ryo Osawa

Written by Ryo Osawa

Software Engineer. Live in San Francisco. Coffee addict.