In the following snippet, I wish to have a class variable foo
. Subclasses of A
may or may not override the static variable foo
. In the following example it prints undefined
for all three print statements. How can I refer to the most recent static variable in the MRO? (i.e. I can't hardcode the class name in run
because it maybe be subclassed).
class A {
static foo = "default"
run() {
console.log(this.foo) // this needs to point to this class here
}
}
class B extends A {
}
class C extends A {
static foo = "overridden"
}
a = new A()
a.run() // Expecting "default"
b = new B()
b.run() // Expecting "default"
c = new C()
c.run() // Expecting "overridden"