es6 access static method of child class from static method of parent?

eagspoo

I want to do this:

class Parent {
    static myMethod1(msg) {
    // myMethod2 is undefined
    this.constructor.myMethod2(msg);
  }
}

class Child extends Parent {
  static myMethod2(msg) {
    console.log('static', msg);
  }
}

Child.myMethod1(1);

But it doesn't work. Is this possible some other way? I don't want to hard code Child.myMethod2 in Parent which I know would work since I want random child classes to be able to define/override the static method but call that method from the parent without prior knowledge of which class is the child.

Estus Flask

myMethod2 is undefined because the code is wrong. this is class constructor in static methods, and this.constructor is the constructor of a constructor, i.e. Function. It should be:

class Parent {
  static myMethod1(msg) {
    this.myMethod2(msg);
  }
}

This is antipattern, because Parent doesn't have myMethod2, and Parent.myMethod1() will result in error. It should either contain no-op myMethod2, or be labeled as abstract class to never be accessed directly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot access child static property from parent static method in PHP

From Dev

How to access parent class instance method from child class static method in python?

From Dev

getting child class from static method of parent class in Java

From Dev

Access to property defined in child class from static method in the parent class - javascript

From Dev

Calling ES6 class constructor from class static method

From Javascript

Call a child method from a parent class in ES6

From Dev

Typescript: Override static factory method of parent Class in Child method

From Dev

Python get child class inside parent class static/class method

From Dev

Calling constructor of parent class in static method of child class

From Dev

How to return non static variable from static method ES6 Class

From Java

Redefining static method in child class

From Dev

How to create a child class in TypeScript using parent static method?

From Dev

What child-class caused static parent method? Or mission impossible?

From Dev

How to call parent static method using child class in kotlin?

From Dev

Static method in parent class does not allow child to add a non static method?

From Dev

Access variable in another method from static method in same class

From Dev

How can i call child's static method from parent's static method?

From Dev

TypeScript: access static method of a class

From Dev

Instantiating child class from a static method in base class, using TypeScript

From Dev

Return a new instance of Child class from base class static method

From Dev

Why can I get access to static method of my super class from child instance?

From Dev

Create mockImplementation for es6 Class' Static Method in Jest

From Dev

Access static method from static variable

From Dev

Access method of private class inside static inner class from main()

From Dev

How to refer overriden static method from parent class constructor?

From Dev

Changing parent's class static method from a subclass

From Dev

JS & ES6: Access static fields from within class

From Dev

How to access a child class method from a parent class?

From Dev

Using the child's static variable in a parent's static method

Related Related

  1. 1

    Cannot access child static property from parent static method in PHP

  2. 2

    How to access parent class instance method from child class static method in python?

  3. 3

    getting child class from static method of parent class in Java

  4. 4

    Access to property defined in child class from static method in the parent class - javascript

  5. 5

    Calling ES6 class constructor from class static method

  6. 6

    Call a child method from a parent class in ES6

  7. 7

    Typescript: Override static factory method of parent Class in Child method

  8. 8

    Python get child class inside parent class static/class method

  9. 9

    Calling constructor of parent class in static method of child class

  10. 10

    How to return non static variable from static method ES6 Class

  11. 11

    Redefining static method in child class

  12. 12

    How to create a child class in TypeScript using parent static method?

  13. 13

    What child-class caused static parent method? Or mission impossible?

  14. 14

    How to call parent static method using child class in kotlin?

  15. 15

    Static method in parent class does not allow child to add a non static method?

  16. 16

    Access variable in another method from static method in same class

  17. 17

    How can i call child's static method from parent's static method?

  18. 18

    TypeScript: access static method of a class

  19. 19

    Instantiating child class from a static method in base class, using TypeScript

  20. 20

    Return a new instance of Child class from base class static method

  21. 21

    Why can I get access to static method of my super class from child instance?

  22. 22

    Create mockImplementation for es6 Class' Static Method in Jest

  23. 23

    Access static method from static variable

  24. 24

    Access method of private class inside static inner class from main()

  25. 25

    How to refer overriden static method from parent class constructor?

  26. 26

    Changing parent's class static method from a subclass

  27. 27

    JS & ES6: Access static fields from within class

  28. 28

    How to access a child class method from a parent class?

  29. 29

    Using the child's static variable in a parent's static method

HotTag

Archive