IT:AD:JScript:Private Fields
Prototype And Objects
Prototypes are not superclasses. They are separate objects.
Won't work:
function Shape() {
var area = 50;
this.setArea = function(a) {area = a;};
this.getArea = function() {return area;};
}
function Square(){}
Square.prototype = Shape;
var shape1 = new Square();
var shape2 = new Square();
shape1.setArea(100);
//Unfortunately, shape2.getArea==100, when it should be 50...
Works:
function Square(){}
Shape.call(this);
}
//OR
function Square(){}
this.Shape = Shape;
this.Shape();
}
var shape1 = new Square();
var shape2 = new Square();
shape1.setArea(100);
//Correct: shape2.getArea==50...