JavaScript Object Methods

Back to JS page

Table of Contents

What are Object Methods?

Methods are actions that can be performed on objects. A method is a function stored as a property of an object:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>What are Object Methods?</h4>
<p id="demo"></p>

<script>
const person = {
  firstName: "John",
  lastName: "Doe",
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

document.getElementById("demo").innerHTML =
  "Method result: " + person.fullName();
</script>

</body>
</html>

Example 1

Result View Example

The this Keyword

In object methods, this refers to the owner object. Without this, a function cannot access object properties:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The this Keyword</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30,
  introduce: function() {
    return "Hi, I'm " + this.firstName + " " + this.lastName;
  },
  getAge: function() {
    return "I am " + this.age + " years old.";
  }
};

document.getElementById("demo1").innerHTML = person.introduce();
document.getElementById("demo2").innerHTML = person.getAge();
</script>

</body>
</html>

Example 2

Result View Example

Accessing Object Methods

Object methods can be accessed with or without parentheses. Without parentheses, you get the function definition:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Accessing Object Methods</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
const person = {
  firstName: "John",
  lastName: "Doe",
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

// Access method WITH parentheses
document.getElementById("demo1").innerHTML =
  "With parentheses: " + person.fullName();

// Access method WITHOUT parentheses (returns function)
document.getElementById("demo2").innerHTML =
  "Without parentheses: " + person.fullName;
</script>

</body>
</html>

Example 3

Result View Example

Adding a Method to an Object

You can add methods to an existing object:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Adding a Method to an Object</h4>
<p id="demo"></p>

<script>
const person = {
  firstName: "John",
  lastName: "Doe"
};

// Add a method
person.fullName = function() {
  return this.firstName + " " + this.lastName;
};

// Add another method
person.greet = function(name) {
  return "Hello " + name + ", I'm " + this.firstName;
};

document.getElementById("demo").innerHTML =
  person.fullName() + "<br>" +
  person.greet("Jane");
</script>

</body>
</html>

Example 4

Result View Example

Using Methods for Calculations

Object methods can perform calculations using object properties:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Using Methods for Calculations</h4>
<p id="demo"></p>

<script>
const rectangle = {
  width: 10,
  height: 5,
  area: function() {
    return this.width * this.height;
  },
  perimeter: function() {
    return 2 * (this.width + this.height);
  },
  isSquare: function() {
    return this.width === this.height;
  }
};

document.getElementById("demo").innerHTML =
  "Width: " + rectangle.width + "<br>" +
  "Height: " + rectangle.height + "<br>" +
  "Area: " + rectangle.area() + "<br>" +
  "Perimeter: " + rectangle.perimeter() + "<br>" +
  "Is square? " + rectangle.isSquare();
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference