Sometimes, we want to call a method from another method in the same class with JavaScript.
In this article, we’ll look at how to call a method from another method in the same class with JavaScript.
How to call a method from another method in the same class with JavaScript?
To call a method from another method in the same class with JavaScript, we use this
.
For instance, we write
class MyClass {
myTest() {
console.log("it works");
}
runMyTest() {
this.myTest();
}
}
const myClass = new MyClass();
myClass.runMyTest();
to call this.myTest
in runMyTest
to call the myTest
method in runMyTest
.
Conclusion
To call a method from another method in the same class with JavaScript, we use this
.