//I have this base Rectangle constructor function
function Rectangle (length, width){
this.length = length;
this.width = width;
}
Rectangle.prototype.getArea = function (){
return this.length * this.width;
};
//Creating Square constructor function that will inherit from Rectangle...
function Square(size){
this.length = size;
this.width = size;
}
Square.prototype = new Rectangle();
Square.prototype.constructor = Square;
//creating rectangle and square instances
var rect = new Rectangle(5, 10);
var square = new Square(6);
console.log(rect.getArea()); //50
console.log(square.getArea()); //36
console.log(Rectangle.prototype.isPrototypeOf(Square.prototype)); //true
console.log(Rectangle.prototype.isPrototypeOf(rect)); //true
console.log(Square.prototype.isPrototypeOf(square)); //true
my question is when i do the below console.log()
, I expected it to print false
. However, I got true
.
console.log(Rectangle.prototype.isPrototypeOf(square)); //true
1) Does this mean isPrototypeOf
goes to multiple levels?
2) if isPrototypeOf
goes multiple levels, what is the point of using isPrototypeOf
instead of using instanceof
?
I've read this Why do we need the isPrototypeOf at all? but didn't understand how it applies in my use-case.