Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Object Methods

JS Object Methods

JavaScript methods are actions that can be performed on objects.

A JavaScript method is a property containing a function definition. In short methods are functions stored as object properties.

					 
        
          const person = {
            firstName: "John",
            lastName: "Doe",
            id: 5566,
            fullName: function() {
              return this.firstName + " " + this.lastName;
            }
          };
        
      
What is this?

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.

Accessing Object Methods

You access an object method with the following syntax:

					 
        
          objectName.methodName()
        
      

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 ().

					 
        
          name = person.fullName();
        
      

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

					 
        
          name = person.fullName;
        
      
Adding a Method to an Object

Adding a new method to an object is as easy as adding propertes.

					 
        
          person.name = function () {
            return this.firstName + " " + this.lastName;
          };
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved