Solution
Just found the solution, use this
But not sure if it is the right way, so pls review/correct if necessary.
class SuperClass {
static calc1() {
return 5;
}
static calc2() {
return 10;
}
static calcAll() {
//reference to this, bound to class itself, because of 'static'
return this.calc1() + this.calc2();
}
}
class SubClass extends SuperClass {
//override
static calc1() {
return 1000;
}
}
console.log(SuperClass.calcAll()); //5+10 = 15
console.log(SubClass.calcAll()); //1000+10 = 1010
Optional info:
Some things i found by the way
override instance methods, is the same:
class SuperClass {
calc1() {
return 5;
}
calc2() {
return 10;
}
calcAll() {
//reference to this, bound to instance
return this.calc1() + this.calc2();
}
}
class SubClass extends SuperClass {
//override
calc1() {
return 1000;
}
}
console.log(new SuperClass().calcAll()); //5+10 = 15
console.log(new SubClass().calcAll()); //1000+10 = 1010
To use static methods in instance methods, or to map static methods on instance methods use current constructor reference.
class SuperClass {
static calc1() {
return 5;
}
calc2() {
return 10;
}
calcAll() {
//'this' bound to instance, and we get current! (e.g. SubClass) class, by .constructor call
return this.constructor.calc1() + this.calc2();
}
}
class SubClass extends SuperClass {
//override
static calc1() {
return 1000;
}
}
console.log(new SuperClass().calcAll()); //5+10 = 15
console.log(new SubClass().calcAll()); //1000+10 = 1010
map statics methods to instance methods:
class SuperClass {
static calc1(num) {
return num;
}
calc1(...args) {
//reference to static
return this.constructor.calc1.apply(this.constructor, args);
}
/* alternative:
calc1(num) {
//reference to static
return this.constructor.calc1(num);
}
*/
}
class SubClass extends SuperClass {
//override
static calc1(num) {
return num + 1000;
}
}
//call statics
console.log(SuperClass.calc1(5)); //5
console.log(SubClass.calc1(5)); //5+1000 = 1005
//call instance
console.log(new SuperClass().calc1(5)); //5
console.log(new SubClass().calc1(5)); //5+1000 = 1005