Categories
JavaScript Answers

How to create a method for a custom object in JavaScript?

Spread the love

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.

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 *