The first part of this topic has explained what prototypes are and how they are used. Recall that, JS chooses rather to link the prototype to the blank object than to duplicate it. This results in the share of property code and method code - the more they share, the less memory they require. Carefully read that they share the code, not the memory space. Looking at the following example:
function X() {
}
X.prototype = {
x: 0,
getX: function(){
return this.x;
},
setX: function(new_x){
this.x = new_x;
}
}
myNum = new X();
yourNum = new X();
myNum.setX(8);
document.write(yourNum.getX()); // print "0"
After the 2 objects are created, they contain nothing. The call setX() on “myNum”, which is available upon its prototype chain, sets “this.x” to the specified number. The “this” keyword in this context references to the object “myNum”, not the prototype. It does not change the “x” in the prototype. You might check it out by yourself. The next line calling getX(), is also found in its prototype chain. The reference to “this.x” does not directly mean the “x” in the prototype, but it is indirectly referenced because the property “x” is not in the object itself. That is, “this.x” is later found in its prototype chain.
The art of JS language design, as you see, is extremely fascinating. The memory used is minimized as less as possible. However, the following code can cause a logical error that is unknown to most programers.
function X(){
}
X.prototype = new function(){
var x;
this.getX = function(){
return x;
}
this.setX = function(new_x){
x = new_x;
}
}
myNum = new X();
yourNum = new X();
myNum.setX(8);
document.write(yourNum.getX()); // print "8"
Differently coded, the sematic is almost the same except that “x” is set local or private. You might think that the above is better. However, unlike local variables in the constructor that are saved as separate closures for separate objects, “x” is shared among objects that link it as their prototype. The reason is that, the prototype is an object that has been instantiated long before. The save of the closure was done only once by the time the prototype was instantiated. As a result, there is only one closure shared among objects. So, be very careful if you wish to use private variables, which is not supported directly by JS.
Prototypes are not only used for object instantiation, but also for primitive types. Surprising? Remember that, reference types and corresponding primitive types are differentiated only by the presence of the “new” keyword. But, they do share the same constructor. The “new” keyword simply gives you a blank object linked to its prototype. Without “new” keyword, a memory is also allocated to that primitive value and linked to the prototype as well. The “this” keyword refer to the primitive data instead of object. That is a reason why we have a wonderful code work nicely.
Number.prototype.isOdd = function(){
return (this%2)? true : false;
}
var a = 7;
if(a.isOdd())
document.write("Is it really odd?");
By now, I assume that you are familiar with prototypes. The following referencing is meant to check your understanding. It can confuse you more if you do not really understand. Think of what they are before execute it. The weird syntax is used by mean to guide you to the answer. There is a saying that thousands words cannot better explain than a picture. Looking up this picture will also guide you to the answer. I got it from MollyPages.org
var Foo = Function("", "this.x = 6;");
var myObj = new Foo();
var myList = [
myObj.__proto__,
myObj.__proto__.constructor,
Foo.prototype,
Foo.__proto__,
Foo.__proto__.constructor,
Object.prototype.__proto__,
myObj.__proto__.constructor.__proto__.constructor,
]
for(index in myList){
document.write("<li>" + myList[index] + "</li>");
}

กุบ้าได้ที่แล้ว หึหึ . . .
so d0m3z

