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.
Note that while x may be private, it’s shared for all instances. Might be useful for private constants, but not for variables.
I don’t believe your final solution is any more memory efficient than the 3rd example you gave.
You are creating a new object/prototype every time you instantiate a new f1(), thus eliminating the benefit of prototyping all together.
Hi Ryan,
I believe that you havent looked at the last solution carefully enough, because what you say is simply not true.
First of all, of course every time someone instantiates a new f1(), a new object is creates, that is simply the meaning of instantiation.
What that makes this solution more efficient is the fact that the getter method – getX() is created only once, and not on every instantiation. Not only I do not eliminate the use of prototyping I am actually using it for my benefit.
Perhaps I should have been more clear regarding that.
BTW, If you still don’t believe me that the getter method is created only once, please take a look at this example which I have created especially for you:
http://jsfiddle.net/aviyehuda/a6x4N/
That fiddle helped a lot to clarify.. you should consider rewriting your post to make this more clear
very nice post.. thank you so much. dude!