To add a method to a JavaScript object.

Javascript: Adding Methods to Objects

We can add functions to objects, as properties, and when that is done they are referred to as methods.

// We will create an object called someObject and a property of that object // will be a function called "add". The "add" function is now a METHOD. var someObject =  name: "Nick Coughlin", height: "5'11\"", hair: "brown", age: 30, isCool: true, pets: ["max", "jewel", "bailey", "lola"], add: function(x,y) return x + y; > > //to call the function we now must call it as a property of the object someObject someObject.add(10,140) //150

So why would we want to add methods to objects? Why not just keep our functions separate?

// adding methods to objects becomes useful for organizing functions that are similar, // so that we can avoid namespace collisions. var dog =  name: "lola", age: 5, sound: function() return("woof woof"); > > var cat =  name: "Mr. Puff", age: 8, sound: function() return("meow meow"); > >

And if we run that in the console we get:

meow meow, woof woof in console

and an alternate way to add functions to objects is like this:

// we create an empty object named comments var comments =  > // and then we add methods to the object comments.add = function(x,y) return x + y; > comments.subtract = function(x,y) return x - y; >

and we can see that that works in the console and returns the expected values:

adding and subtracting from comments

// The "this" keyword is used inside of a method to refer to the object that it is contained inside of // It is a way of sharing data inside of an object // Lets take a simple array of comments inside of an object var article = >; article.comments = ["great article", "I hate your face", "I find this content offensive"]; // and now let us write a function that prints the comments article.printComments = function() this.comments.forEach(function(el) //el is short for element console.log(el); >); > // so we just added a function to the object "article" called printComments that // references the object that it's inside of (articles) and then the array inside // that it wants to loop over (comments)

and that gives us the following result:

a list of comments

Amazon Ad Analytics For Humans

Advertising reports automatically saved and displayed beautifully for powerful insights.

Источник

How to add a method to a JavaScript object?

A JavaScript object is an entity which has properties. A property can be a variable or a method which define state and behavior of the object. A method is a property of an object that adds behavior to an object.

We can add a method to a JavaScript object using object prototype. All JavaScript objects get their attributes and methods from a prototype.

Let’s understand this concept better with the help of examples further in this article.

Syntax

The syntax to add a method to the JavaScript object using Object Prototype is −

functionName.prototype.newMethodName = function()<> or functionName.prototype.newMethodName = function(param1,param2. paramN)<>
  • functionName is the existing function name
  • newMethodName is the method name to be added.
  • param1, param2..paramN are the parameters that are to be passed to the new method.

Example 1

This is an example program to add a method to an object by extending the behavior of Calculator function prototype with add.

       

To add a method to a JavaScript object.

On executing the above code, the below output is generated.

Example 2

This is an example program to add a method to JavaScript object.

        

To add a method to a JavaScript object.

On executing the above code, the below output is generated.

Example 3

This is an example program to create a function which returns an object. In this example, a .mul is created as a property of the object. Each of the new object created has a .mul function created for them. In this example, method is a parameter to the function.

       

To add a method to a JavaScript object.

On executing the above code, the below output is generated.

Источник

JavaScript Object Methods

In JavaScript, the this keyword refers to an object.

Which object depends on how this is being invoked (used or called).

The this keyword refers to different objects depending on how it is used:

In an object method, this refers to the object.
Alone, this refers to the global object.
In a function, this refers to the global object.
In a function, in strict mode, this is undefined .
In an event, this refers to the element that received the event.
Methods like call() , apply() , and bind() can refer this to any object.

Note

See Also:

JavaScript Methods

JavaScript methods are actions that can be performed on objects.

A JavaScript method is a property containing a function definition.

Property Value
firstName John
lastName Doe
age 50
eyeColor blue
fullName function()

Methods are functions stored as object properties.

Accessing Object Methods

You access an object method with the following syntax:

You will typically describe fullName() as a method of the person object, and fullName as a property.

The fullName property will execute (as a function) when it is invoked with ().

This example accesses the fullName() method of a person object:

Example

If you access the fullName property, without (), it will return the function definition:

Источник

Читайте также:  What is hash function in java
Оцените статью