Platinum Partner
architects,html5,javascript,tips and tricks

JavaScript encapsulation & the module pattern

 Encapsulation is one of the key features of object oriented programming languages.
In languages like Java, it is very straight forward concept to implement.

Since I know JavaScript is considered an OO language, I decided to try to understand once and for all how to implement encapsulation in JavaScript correctly.

Let’s say we have a function with one private field and one public field:
function f1() {
	var x=3; //private	
	this.y=4; //public
}

//alert(x); // error - x is undefined
//alert(y);// error - y is undefined
alert(f1.x); //alert undefined
alert(f1.y); //alert undefined
//alert(f1().x); //error - f1() does not return an object with property x
//alert(f1().y); //error - f1() does not return an object with property y

The fields are of course undefined since we need to call f1() in order for them to have values.

Now let’s create an object from that function:

var i1 = new f1();

//alert(x); // error 
//alert(y);// error 
alert(i1.x);//alert undefined since it is private
alert(i1.y); //alert 4

After instancing, x and y both have values, but of course, only y is public.

Now lets encapsulate field x.

function f1() {
	var x=3; //private	
	this.y=4; //public

	this.getX = function(){
		return x;
	}
}

var i1 = new f1();

//alert(x); //error
alert(i1.x); //alert undefined
alert(i1.getX()); //alert 3
This is the most basic encapsulation and it works.

But this approach has a problem, f1() is in fact a kind of constructor. Whenever an instance will be created than the getter method will also be created.
This approach is not the preferred way to create methods in JavaScript.

The preferred approach in JavaScript is to create the methods attached to the prototype. In this way they will only be created once.

var f1 = function(){
	var x=3; //private	
	this.y=4; //public
}

f1.prototype.getY = function(){
	return y;
}

/* error
f1.prototype.getX = function(){
	return x; //x is not a public field
}
*/

But this approach also has a problem, we cannot use it for private fields.

Let’s consider using a module

f1 = (function (){
	var x=3;

	var module = function (){
		this.getX = function(){
			return x;
		}
	}

	return module;
})();

var i1 = new f1();
alert(i1.x); //undefined
alert(i1.getX()); //3

There is a function which creates a module with the getter.
But here we have the same problem as the first example, getX() is created for every instance.

The solution is in fact a combination of the previous 2 examples:

f1 = (function (){
	var x=3;

	var module = function (){}
	
	module.prototype.getX = function(){
		return x;
	}

	return module;
})();

var i1 = new f1();

//alert(x); undefined
alert(f1.x); //undefined
alert(i1.getX()); //3

Here you have a strict encapsulation which is also efficient in memory.



Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}