Table of Contents
In JavaScript, the this keyword refers to an object.
The this keyword refers to different objects depending on how it is used:
this refers to the objectthis refers to the global objectthis refers to the global objectthis is undefinedthis refers to the element that received the eventcall(), apply(), and bind() can refer this to any objectWhen 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>

Result View Example
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>

Result View Example
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>

Result View Example
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>

Result View Example
Document in project
You can Download PDF file.