As far as I know, the __proto__ property of a constructor is considered deprecated. Is there a better way to access a property of the parent class from a created instance of its subclass?
Example:
In the following example, the requested property is cls.
class Vehicle {
constructor () {
var div = document.createElement("div");
var cls = this.constructor.__proto__.cls + " " + this.constructor.cls;
div.setAttribute("class", cls);
document.body.appendChild(div);
}
}
class Car extends Vehicle {}
class Motorcycle extends Vehicle {}
Vehicle.cls = "vehicle";
Car.cls = "car";
Motorcycle.cls = "motorcycle";
let vehicle = new Vehicle();
let car = new Car();
let bike = new Motorcycle();
.vehicle {
width: 50px;
height: 50px;
display: inline-block;
background-color: red;
}
.car {
background-color: green;
}
.motorcycle {
background-color: blue;
}
Object.getPrototypeOf()? But why do you need to access the super class? If you are doing this a method, you can just usesuper. I don't think the code that works with an instance of a class should make any assumptions about its implementation.