I would like to have a base class and an inheriting class. The base class should provide some general functionality which depends on the properties of the inheriting class.
=>How can I access the properties of the inheriting class in the base class?
Below is some example to illustrate the question (My question is not how to define an enum in JavaScript. The example is just for illustration.).
Example base class:
export default class Enum {
constructor(name){
this.name = name;
}
}
Enum.values = function(){
return Object.values(INHERITING_CLASS);
};
Enum.forName = function(name){
for(var enumValue of INHERITING_CLASS.values){
if(enumValue.name === name){
return enumValue;
}
}
throw new Error('Unknown value "' + name + '"');
}
Example inheriting class:
import Enum from './enum.js';
export default class ColumnType extends Enum {
constructor(name, clazz){
super(name);
this.associatedClass = clazz;
}
}
ColumnType.Integer = new ColumnType('Integer', Number);
ColumnType.Double = new ColumnType('Double', Number);
ColumnType.String = new ColumnType('String', String);
I want to be able to access the static values of ColumnType with
ColumnType.values()
where the values
function is provided by the base class Enum. Some for method forName
.
If I would use "Enum" for the placeholder INHERITING_CLASS, the result is not correct.
=> How do I know that ColumnType is the currently inheriting class while being in the scope of the definition of the Enum class?
Edit
Here is a related question:
Get parent class name from child with ES6?
Their answer uses instance.constructor
. However, in my static method I don't have an instance.