Interpret `subclass.__proto__ === parentClass` in JS inheritance?

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.

Square.__proto__ === Polygon is true because Square is created using the extends keyword, which sets the Square class’s prototype (__proto__) to the Polygon class. This means that Square inherits all the properties and methods of Polygon, including the name property.

Similarly, Square.prototype.__proto__ === Polygon.prototype is true because the Square.prototype is set to a new object that inherits from the Polygon.prototype. This allows Square instances to access the methods and properties defined in the Polygon.prototype.