In JavaScript (ES6), how do I programmatically get the name of the super class, in which the "current" code is executed?
Example:
class MySuperClass {
logName() {
console.log(this.XXX()); // always logs "MySuperClass"
}
}
class MySubClass extends MySuperClass {
logName() {
super.logName();
console.log(this.XXX()); // always logs "MySubClass"
}
}
class MySubSubClass extends MySubClass {
logName() {
super.logName();
console.log(this.XXX()); // always logs "MySubSubClass"
}
}
const subSubClass = new MySubSubClass();
console.log(subSubClass);
Expected result from this code would be:
MySuperClass
MySubClass
MySubSubClass
What do I need to do instead of this.XXX()?
Note that I do not want to hard-code the name of the classes as string constants. I want a way that gets the "current" class name programmatically, as the flow trickles up the class tree.
Possible?
What does NOT work is this.constructor.name - it always returns the "lowest" inherited constructor name, in this case, it would yield "MySubSubClass" in all 3 levels.
Edit: One kind of hacky way would be to throw an exception on purpose and parse the stack string to see in which file / class the exception was thrown. But ugh! Is there no clean way?