Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JavaScript encapsulation & the module pattern

DZone's Guide to

JavaScript encapsulation & the module pattern

· Web Dev Zone
Free Resource

Make the transition to Node.js if you are a Java, PHP, Rails or .NET developer with these resources to help jumpstart your Node.js knowledge plus pick up some development tips.  Brought to you in partnership with IBM.

 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.



Learn why developers are gravitating towards Node and its ability to retain and leverage the skills of JavaScript developers and the ability to deliver projects faster than other languages can.  Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Avi Yehuda, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}