I have some trouble of understanding the word 'this' in call function when it is used in subclass with super class like this:
function Rectangle(w, h) {
this.width = w;
this.height = h;
}
Rectangle.prototype.area = function() { return this.width * this.height; }
function PositionedRectangle(x, y, w, h) {
Rectangle.call(this, w, h);
this.x = x;
this.y = y;
}
PositionedRectangle.prototype = new Rectangle();
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;
PositionedRectangle.prototype.constructor = PositionedRectangle;
PositionedRectangle.prototype.contains = function(x, y) {
return (x > this.x && x < this.x + this.width &&
y > this.y && this.y + this.height);
}
var r = new PositionedRectangle(2, 2, 2, 2);
document.write(r.contains(3, 3)); // 4
document.write("<br>" + r.area()); // 4
document.write("<br>" + r.x + ", " + r.y + ", " + r.width + ", " + r.height + "<br>"); // 2, 2, 2, 2
document.write(r instanceof PositionedRectangle && r instanceof Rectangle && r instanceof Object); // true
Now this part I don't understand:
Rectangle.call(this, w, h);
in PositionedRectangle class. What 'this' stands for? With what can I replace it so the code can work normally? I thought first 'this' is same as Rectangle and I tried to replace it with name Rectangle but it didn't work. Than I thought it's a PositionedRectangle subclass I tried to replaace it with PositionedRectangle.
I read that 'this' meaning depends on how it is called and I know the first argument in call function represents an object, but when the value of that object is 'this' I don't understand what that actually represents.
And I am still new in JavaScript as you can see.
I appreciate your help.