First, some code to set the stage:
var instances = [];
class Parent {
static doImportantStuff() {
console.log( 'Parent doing important stuff' );
return false;
}
static init() {
if ( this.doImportantStuff() )
instances.push( new this() );
}
}
class Child1 extends Parent {
static doImportantStuff() {
console.log( 'Child 1 doing important stuff' );
if (someCondition) return true;
return false;
}
}
class Child2 extends Parent {
static doImportantStuff() {
console.log( 'Child 2 doing important stuff' );
if (someOtherCondition) return true;
return false;
}
}
The idea is that there's a class Parent
that several Child
classes extend. Initialization of the Child
classes is mostly the same, but each has their own implementation of doImportantStuff()
, the return value of which indicated whether that particular Child
should be instantiated.
So far this has worked in each transpiler I've tried, because this
in the Parent.init()
function refers to the constructor of the Child
class. However I haven't found any documentation saying one way or the other about referring to a static method overridden by a child class, so the question is, can I rely on this always being so? Or, is there some other way to do this?