JavaScript this Keyword

Back to JS page

Table of Contents

What is this?

In JavaScript, the this keyword refers to an object.

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

this in an Object Method

When used in an object method, this refers to the object itself:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript this Keyword</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>this in an Object Method</h4>
<p id="demo"></p>

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

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

</body>
</html>

Example 1

Result View Example

Object Method Binding

Object method binding refers to how this is bound when calling a method on an object.

When a function is called as a method of an object, this is bound to that object:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript this Keyword</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Object Method Binding</h4>
<p id="demo1"></p>
<p id="demo2"></p>

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

// Method called on the object - "this" refers to person
document.getElementById("demo1").innerHTML = person.fullName();

// When method is extracted, "this" is lost
const myMethod = person.fullName;
document.getElementById("demo2").innerHTML = "Lost this: " + myMethod();
</script>

</body>
</html>

Example 2

Result View Example

Explicit Function Binding

The call(), apply(), and bind() methods can explicitly set what this refers to:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript this Keyword</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Explicit Function Binding</h4>
<p id="demo"></p>

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

const person2 = {
  firstName: "Jane",
  lastName: "Smith"
};

const person3 = {
  firstName: "Bob",
  lastName: "Johnson"
};

// Using call() to explicitly set "this"
document.getElementById("demo").innerHTML =
  "call() person2: " + person1.fullName.call(person2) + "<br>" +
  "call() person3: " + person1.fullName.call(person3);
</script>

</body>
</html>

Example 3

Result View Example

Function Borrowing

Function borrowing allows an object to use a method from another object:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript this Keyword</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Function Borrowing</h4>
<p id="demo"></p>

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

const member = {
  firstName: "Jane",
  lastName: "Smith"
};

const member2 = {
  firstName: "Bob",
  lastName: "Johnson"
};

// Borrow the fullName method from person using bind()
const memberFullName = person.fullName.bind(member);
const member2FullName = person.fullName.bind(member2);

document.getElementById("demo").innerHTML =
  "Member 1: " + memberFullName() + "<br>" +
  "Member 2: " + member2FullName();
</script>

</body>
</html>

Example 4

Result View Example

Document

Document in project

You can Download PDF file.

Reference