0

I want to be able to access a method from both the instance and the class:

class AClass {
  // I want to make this both static and normal
  method() {
    console.log("Method called");
  }
}

I would expect to be able to access static methods from the instance (like in python with @staticmethod) but I can't as it gives an error:

class AClass {
  static method() {
    console.log("Method called");
  }
}

AClass.method();  // this works
(new AClass).method();  // this doesn't work

Similar questions

  • Call static methods from regular ES6 class methods This question is not a duplicate of that question because I want the same name to both a static method and an instance method. That question is about calling a static method from an instance method (different name)
MarcellPerger
  • 405
  • 3
  • 13
  • No, I want the *same name* to both a static method and an instance method. That question is about calling a static method from an instance method (different name) – MarcellPerger Apr 11 '23 at 18:32
  • @MarcellPerger then just name them the same? – VLAZ Apr 11 '23 at 18:38
  • So, why not just assign a property to the instance in the constructor? `constructor() { this.method = AClass.method; }` – JDB Apr 11 '23 at 19:28
  • If I have lots of static methods on the object, the constructor could get very long and I would prefer to keep the each function in one area of the code, but its still better than nothing. – MarcellPerger Apr 11 '23 at 19:35
  • 1
    "*`(new AClass).method();`, this doesn't work*" - this **should** not work. Why would you call a static method, which is not related to instances, on an instance? JavaScript is not Java, just use `AClass.method()` that works. If you really need to, `(new AClass).constructor.method()`. – Bergi Apr 11 '23 at 20:22

1 Answers1

0

Create a non-static method of the same name and call the static method from the non-static method.

class AClass {
  static method() {
    console.log("Method called");
  }
  method() {
    AClass.method();
  }
}

AClass.method();  // this works
(new AClass).method();  // this now works
Rojo
  • 2,749
  • 1
  • 13
  • 34
  • 1
    This is in almost the same as [Berhi's answer here](https://stackoverflow.com/a/28648214) but with the crucial difference that it's worse, since it will not respect inheritance. Consider `class A { static method() { /* logic for a */ } }` and then a `class B extends A { static method() { /* logic for B */} }` By directly calling the static method from `A` the one defined in `B` will be ignored. Whereas with `this.constructor.method()` the child method, if any, would be used. Demo: https://jsbin.com/wucaxifali/1/edit?js,console – VLAZ Apr 12 '23 at 09:51
  • @VLAZ [Avoid answering questions with comments](https://stackoverflow.com/help/privileges/comment) – Rojo Apr 12 '23 at 12:36
  • 1
    I've answered the question with a duplicate already. This comment was a critique of the answer pointing out a flaw that is already solved in the duplicate. Speaking of: avoid answering duplicates. – VLAZ Apr 12 '23 at 13:24