English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript basic tutorial

JavaScript object

JavaScript function

JS HTML DOM

JS browser BOM

AJAX basic tutorial

JavaScript reference manual

JavaScript Object Methods

Methodis associated with an object, or a related function; a method is an object that is a property of a function.

The definition of a method is the same as that of a regular function, the difference being that they must be assigned as properties of an object.

Access JavaScript method

To retrieve an object method, you will call it in the same way as a regular function, just attach it to the object variable.

// Create an object
var user = {
  firstName: "Seagull",
  lastName  :  "an",
  age: 22,
  location: "New Delhi",
  getName  :  function() {
 return this.firstName + "" + this.lastName;
  }
};
//Access the getName() method
user.getName();
Test and see‹/›

If you access a method without parentheses, it will return the function definition:

user.getName;
Test and see‹/›

usingthisAs an object reference

JavaScript has a special keyword this, which you can use in methods to refer to the current object.

You may have noticed that our methods are a bit strange. Take this example:

  getName: function() {
   return this.firstName + "" + this.lastName;
  }

The this keyword refers to the internal current object where the code is written-So in this case, this is equivalent touser.

In other words, this.firstName representsThis objectthe firstName property.

You can accessJS keywordTutorialIn JSLearn more about the this keyword.

Add a new method

To add a new method to an object, you can use the assignment operator (=) to assign a new function to the property.

This example adds the "greet" method to the user object:

user.greet = function() {
    return "Hello World";
};
Test and see‹/›

Getters and Setters

ECMAScript 5(2009) Introduced Getters and Setters.

Getter is a method used to retrieve the value of a specific property.

Setter is a method used to set the value of a specific property.

You can define getters and setters on any predefined core object or user-defined object that supports adding new properties.

JavaScript Getter (get keyword)

This example uses get locProperty aslocationProperty value:

//Create an object
var user = {
  firstName: "Seagull",
  lastName: "Anna",
  age: 22,
  location: "New Delhi",
  get loc() {
     return this.location;
  }
};
//Display data from the object
document.getElementById("para").innerHTML = user.loc;
Test and see‹/›

JavaScript Setter (set keyword)

This example uses set locProperty aslocationProperty value:

// Create an object
var user = {
  firstName: "Seagull",
  lastName: "Anna",
  age: 22,
  location: "New Delhi",
  set loc(x) {
     this.location = x;
  }
};
// Using setter to set object properties
user.loc = "Goa";
// Display data from the object
document.getElementById("para").innerHTML = user.location;
Test and see‹/›

Difference between Function function and Getter?

The following two examples show the difference between function and getter:

Example1(Using Function):
//Create an object
var user = {
  firstName: "Seagull",
  lastName: "Anna",
  age: 22,
  location: "New Delhi",
  fullName: function() {
      return this.firstName + "" + this.lastName;
  }
};
// Display data from the object
document.getElementById("para").innerHTML = user.fullName();
Test and see‹/›
Example2(Using Getter):
// Create an object
var user = {
  firstName: "Seagull",
  lastName: "Anna",
  age: 22,
  location: "New Delhi",
  get fullName() {
     return this.firstName + "" + this.lastName;
  }
};
//Display data from the object
document.getElementById("para").innerHTML = user.fullName;
Test and see‹/›

Example1 Taking fullName asFunctionAccess: user.fullName().

Example2 Taking fullName asPropertyAccess: user.fullName.

Using Getters and Setters:

  • It provides a simpler syntax

  • It allows the syntax for properties and methods to be the same

  • It ensures better data quality

  • It is very useful for backend processing

Object.defineProperty()

The Object.defineProperty() method can also be used to add getters and setters.

Syntax:
Object.defineProperty(object, property, {value : value})

Let's take the 'counter' object as an example:

var counter = {i : 0};
Object.defineProperty(counter, "increment", { 
   get: function() {this.i++;}
});
Object.defineProperty(counter, "decrement", { 
   get: function() {this.i--;}
});
Object.defineProperty(counter, "reset", { 
   get: function() {this.i = 0;},
});
Object.defineProperty(counter, "add", {
   set: function (value) {this.i += value;}
});
Object.defineProperty(counter, "subtract", {
   set: function (value) {this.i -= value;}
});
counter.reset;
counter.add = 25;
counter.increment;
Test and see‹/›