Categories
JavaScript Answers

How to call a method from another method in the same class with JavaScript?

Spread the love

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.

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 *