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.