It's so static
fields/accessors/methods can be looked up on the parent class if it's not found on the child, for example:
class Polygon {
constructor() {
this.name = "Polygon";
}
static staticPolygonMethod() {
return "staticPolygon";
}
}
class Square extends Polygon {
constructor() {
super();
}
}
console.log(Square.staticPolygonMethod()); // staticPolygon
As static fields/accessors/methods are added to the class itself, the staticPolygonMethod
is set on Polygon
class/function itself.
In this example above, staticPolygonMethod
is not found on the Square
class/function, so its [[Prototype]]
is checked for the property, which is set to Polygon
, as you've seen with the Square.__proto__ === Polygon
check. Because staticPolygonMethod
exists on the Polygon
class/function, it can now be found correctly in Square
's prototype chain and thus used as expected on Square
.