I have a parent class in JS that has some properties of which some are based on the class name of the child classes:
ParentClass.js
export default class ParentClass {
constructor(className) {
this.className = className;
this.classNameLowerCase = className.toLowerCase();
}
}
In a child class I extend the parent class and use super()
for the constructor invocation where I will pass the class name of the child class.
ChildClass.js
import ParentClass from "./ParentClass.js";
class ChildClass extends ParentClass {
constructor() {
super("ChildClass");
}
}
As you can see the child class is called ChildClass and I also pass this name as a string in super()
. I prefer to get the class name of the child class with a funtion (without havind to repeat myself). Without using super()
I could use this.constructor.name
to get the name of the class, but inside super()
this
is not allowed and neither anywhere before the parentclass constructor invocation.
How can I get the child class name to use as an argument inside super()
?