Why is Square.__proto__ === Polygon
true?
Why is Square.__proto__ === Polygon
true?
class Polygon {
constructor() {
this.name = "Polygon";
}
}
class Square extends Polygon {
constructor() {
super();
}
}
console.log(Square.__proto__ === Polygon); // true why?
console.log(Square.prototype.__proto__ === Polygon.prototype); // true
The first print statement returns true because Square.__proto__
is an object that links the Square
constructor to the Polygon
constructor. This link is established through the extends
keyword when creating the Square
class. Square
inherits all the properties and methods of Polygon
, including the name
property.
The second print statement returns true because Square.prototype.__proto__
is an object that links the Square.prototype
to the Polygon.prototype
. This link is established when the Square
constructor is created and the Square.prototype
is set to a new object that inherits from the Polygon.prototype
.