2

How to override static method in the subclass, and let them be still used by superclass methods?
Here is the code example:

    class SuperClass {
      static calc1() {
        return 5;
      }

      static calc2() {
        return 10;
      }

      static calcAll() {
        //here must be something else, instead of SuperClass reference
        return SuperClass.calc1() + SuperClass.calc2();
      }
    }


    class SubClass extends SuperClass {
      //override
      static calc1() {
        return 1000;
      }
    }

    console.log(SuperClass.calcAll()); //expected 5+10 = 15, got 15
    console.log(SubClass.calcAll()); //expected 1000+10 = 1010, got 15
ya_dimon
  • 3,483
  • 3
  • 31
  • 42

1 Answers1

1

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
Community
  • 1
  • 1
ya_dimon
  • 3,483
  • 3
  • 31
  • 42
  • I'm quite sure it was already discussed on SO, but thanks for sharing your discovery. This `this.constructor.calc1.apply(this, args);` is half-baked ES6 (and it will fail if `calc1` calls other static method). Use `this.constructor.calc1(...args)`. – Estus Flask Jun 08 '16 at 16:59
  • @estus: For example [here](http://stackoverflow.com/q/28627908/1048572) :-) Btw, the `apply` equivalent of the proper call would be `this.constructor.calc1.apply(this.constructor, args)` – Bergi Jun 08 '16 at 19:10
  • @Bergi ok, fixed, thx. Did not found the problem by SO, at that time. The solution was described by you: http://stackoverflow.com/a/28648214/2519073 – ya_dimon Jun 09 '16 at 13:10