Categories
JavaScript Answers

How to call another function within the same object with JavaScript?

Spread the love

Sometimes, we want to call another function within the same object with JavaScript.

In this article, we’ll look at how to call another function within the same object with JavaScript.

How to call another function within the same object with JavaScript?

To call another function within the same object with JavaScript, we can use this to reference the function.

For instance, we write:

const role = {
  test(variable) {
    this.toLog(variable);
  },
  toLog(variable) {
    console.log(variable);
  }
};

role.test(5);

to call role.test with 5.

In test, we call this.toLog to call the toLog function in role.

Therefore, 5 is logged.

Conclusion

To call another function within the same object with JavaScript, we can use this to reference the function.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.