Sometimes, we want to create a method for a custom object in JavaScript.
In this article, we’ll look at how to create a method for a custom object in JavaScript.
How to create a method for a custom object in JavaScript?
To create a method for a custom object in JavaScript, we set a property to a function.
For instance, we write
const newObj = {
met1() {
alert("hello");
},
};
newObj.met1();
to put the met1
method into the newObj
object.
Then we call met1
with newObj.met1();
.
Conclusion
To create a method for a custom object in JavaScript, we set a property to a function.