Object Methods In JavaScript

Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Object Methods In JavaScript.

Object Methods In JavaScript

Object methods in JavaScript are functions that are associated with an object. They are used to add functionality to an object and are defined within the object’s definition. In this article, we will take a closer look at object methods and explore how they can be used in JavaScript with code examples.

One of the most commonly used object methods in JavaScript is the .prototype method. This method allows you to add new properties and methods to an object’s prototype, which means they will be available to all instances of that object. Here is an example of how to use the prototype method to add a method to the Person object:

Code For prototype Method In JS

function Person(name, age) {
    this.name = name;
    this.age = age;
}

Person.prototype.greet = function() {
    console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
}

In this example, the greet method is added to the Person object’s prototype, which means that it will be available to all instances of the Person object. Now we can create a new instance of the Person object and call the greet method:

let p1 = new Person("John", 30);
p1.greet();

Output

Hello, my name is John and I am 30 years old.

Another common object method is the .constructor method. This method is used to create a new instance of an object, and it is called automatically when the object is created.

The constructor function is defined within the object’s definition and can be used to set the object’s initial properties and methods. Here is an example of how to use the constructor method to create a new instance of the Person object:

Code For constructor Method In JavaScript

function Person(name, age) {
    this.name = name;
    this.age = age;
}

let p1 = new Person("John", 30);
console.log(p1.name);
console.log(p1.age);

Output

“John”

30

Object methods can also be used to create custom methods for an object. These methods can be used to perform specific actions on the object or to return specific information about the object. For example, you could create a method that calculates the area of a shape or a method that returns the number of elements in an array.

let myArray = [1, 2, 3, 4, 5];

Array.prototype.count = function() {
    return this.length;
}

console.log(myArray.count());

Output

5

In addition to these standard object methods, JavaScript also provides several built-in object methods, such as .toString() and .valueOf(). These methods are used to convert an object to a different data type or to retrieve the value of an object. Here is an example of how to use the toString() method to convert an object to a string:

let myObject = {name: "John", age: 30};
console.log(myObject.toString());

Output

"[object Object]"

Conclusion

Overall, object methods in JavaScript are an essential part of working with objects and are used to add functionality, make code reusable, and retrieve information about an object. They are an important part of any JavaScript developer’s toolkit, and understanding how to use them.

If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment