What is the difference between calling a function call(), and calling implicit constructor of a inherited class?
Example1:
function DepthRectangle(w, h, d){
Rectangle.call(this, w, h);
this.depth = d;
}
Example2:
function DepthRectangle(w, h, d){
this = new Rectangle(w, h);
this.depth = d;
}
Constructor of Rectangle:
function Rectangle(w, h){
this.width = w;
this.height = h;
}
Well, your second example doesn’t work, because the
thisvalue is not assignable, it isn’t a valid left-hand side expression for assignments.But, suppose that you have:
In this case, the difference would be that the object returned by this function will inherit directly from
Rectangle.prototype, and not fromDepthRectangle.For example, with the above function:
In your first example, using the
callmethod will just execute theRectanglefunction, but with thethisvalue pointing to the object that was created onDepthRectangle, meaning that this object will in fact still inherit fromDepthRectangle.prototype.