Hi - In am learning Javascript and trying to understand why,
the following line :
[COLOR="Sienna"]arguments.callee.superclass[/COLOR]
cannot be replaced by :
this.constructor.superclass
In the following code sample :
( the explanation given is that if someone creates a subclass of PositionedRectangle, then this.constructor will refer to the new subclass constructor, not to PositionedRectangle - but why? 'this' here to my knowledge represents the object 'PositionRectangle' and if not I can't understand why not. )
[I][COLOR="Red"]// Here is a simple Rectangle class.
// It has a width and height and can compute its own area[/COLOR][/I]
function Rectangle(w, h) {
this.width = w;
this.height = h;
}
Rectangle.prototype.area = function( ) { return this.width * this.height; }
[I][COLOR="#ff0000"]// Here is how we might subclass it[/COLOR][/I]
function PositionedRectangle(x, y, w, h) {
[I][COLOR="#ff0000"]// First, invoke the superclass constructor on the new object
// so that it can initialize the width and height.
// We use the call method so that we invoke the constructor as a
// method of the object to be initialized.
// This is called constructor chaining.[/COLOR][/I]
PositionRectangle.superclass = Rectangle;
[COLOR="Sienna"]arguments.callee.superclass[/COLOR].call(this, w, h);
[I][COLOR="#ff0000"]// Now store the position of the upper-left corner of the rectangle[/COLOR][/I]
this.x = x;
this.y = y;
}
[I][COLOR="#ff0000"]// Create a prototype for the subclass that inherits from the prototype
// of the superclass.[/COLOR][/I]
function heir(p) {
function f(){}
f.prototype = p;
return new f();
}
PositionRectangle.prototype = heir(Rectangle.prototype);
PositionRectangle.prototype.constructor = PositionRectangle;