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.