Categories
JavaScript Answers

How to call static method within a class with JavaScript?

Spread the love

Sometimes, we want to call static method within a class with JavaScript.

In this article, we’ll look at how to call static method within a class with JavaScript.

How to call static method within a class with JavaScript?

To call static method within a class with JavaScript, we call the static method with the class.

For instance, we write

class MyClass {
  myNonStaticMethod() {
    console.log("I'm not static.");
    MyClass.myStaticMethod();
  }

  static myStaticMethod() {
    console.log("hey, I'm static!");
  }
}

to call myStaticMethod with MyClass.myStaticMethod() in the myNonStaticMethod method.

Conclusion

To call static method within a class with JavaScript, we call the static method with the class.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *