js call static method from class

Chris :

I have a class with a static method:

class User {
  constructor() {
    User.staticMethod();
  }

  static staticMethod() {}
}

Is there something like this so for static methods (i.e. refer to the current class without an instance).

this.staticMethod()

so I don't have to write the class name 'User'.

Ninjaneer :

From MDN documentation

Static method calls are made directly on the class and are not callable on instances of the class. Static methods are often used to create utility functions.

For more please see=> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static

You can do something like this => this.constructor.staticMethod()); to call static method.

class StaticMethodCall {
  constructor() {
    console.log(StaticMethodCall.staticMethod()); 
    // 'static method has been called.' 

    console.log(this.constructor.staticMethod()); 
    // 'static method has been called.' 
  }

  static staticMethod() {
    return 'static method has been called.';
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to call a static method from another class in JS

From Dev

How to call static class method from a struct?

From Dev

Dynamically call static method on class from string

From Dev

Call a static method from other class in python

From Dev

Call to dialog from View class with static method

From Dev

PHP: is it possible to call a static class method from another static class?

From Dev

Call a non-static method from static inner class

From Dev

Call non static method from different non static class

From Dev

Call ES5 class method from static method

From Dev

Why not call the static method "main" of a class from another class?

From Dev

How to call method in other class from a static class

From Dev

Call Overriden Method from Base Class in JS

From Dev

How to call a class method from the button in js?

From

How to call a static method from a private base class?

From Java

Call Kotlin object with class delegation from Java as a static method

From Dev

How to call non static method from main class

From Dev

Test static method that call another from same class

From Dev

Grails How to call service from static method of domain class?

From Dev

python how to call static method from inside of a class body

From Dev

Call static method from another class without creating object in Java

From

call a static method inside a class?

From Dev

Smarty call a static class method

From

How to call non-static method from static method of same class?

From Java

Cannot access a method call from non-static to non-static class/method

From Dev

React static class/method call and Network call

From Dev

JS - Call to static method with prototype

From Dev

PHP call a static method of a class by class name

From Dev

Call method from class

From Dev

How to call static function in a static class JS?

Related Related

  1. 1

    how to call a static method from another class in JS

  2. 2

    How to call static class method from a struct?

  3. 3

    Dynamically call static method on class from string

  4. 4

    Call a static method from other class in python

  5. 5

    Call to dialog from View class with static method

  6. 6

    PHP: is it possible to call a static class method from another static class?

  7. 7

    Call a non-static method from static inner class

  8. 8

    Call non static method from different non static class

  9. 9

    Call ES5 class method from static method

  10. 10

    Why not call the static method "main" of a class from another class?

  11. 11

    How to call method in other class from a static class

  12. 12

    Call Overriden Method from Base Class in JS

  13. 13

    How to call a class method from the button in js?

  14. 14

    How to call a static method from a private base class?

  15. 15

    Call Kotlin object with class delegation from Java as a static method

  16. 16

    How to call non static method from main class

  17. 17

    Test static method that call another from same class

  18. 18

    Grails How to call service from static method of domain class?

  19. 19

    python how to call static method from inside of a class body

  20. 20

    Call static method from another class without creating object in Java

  21. 21

    call a static method inside a class?

  22. 22

    Smarty call a static class method

  23. 23

    How to call non-static method from static method of same class?

  24. 24

    Cannot access a method call from non-static to non-static class/method

  25. 25

    React static class/method call and Network call

  26. 26

    JS - Call to static method with prototype

  27. 27

    PHP call a static method of a class by class name

  28. 28

    Call method from class

  29. 29

    How to call static function in a static class JS?

HotTag

Archive