Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
JavaScript has a 'this' keyword. Just like other languages, it points to the object it belongs to. But it behaves a little differently as compared to other languages.
Let us discuss 'this' keyword in JavaScript.
this keyword in JavaScript
The value of 'this' is determined by how a function was called during runtime. We cannot assign any particular value to it, and it may be different each time the function is executed. Let us look at the usage of the 'this' keyword in JavaScript.
Code:
const test = {
prop: 10,
func: function() {
// Here 'this' refers to the owner object
return this.prop;
},
};
console.log(test.func());
You can also try this code with Online Javascript Compiler
bind() method creates a new function whose 'this' keyword is set to a specified value.
Constructor Invocation
The function can be used as a constructor when creating an instance of a function object. Here, the 'this' keyword is assigned to the newly created object.
Code:
function Student(name) {
this.name = name;
}
Student.prototype.getName = function () {
// Here 'this' refers to function object
return this.name;
}
var student = new Student('Ninja');
console.log(student.getName());
You can also try this code with Online Javascript Compiler
We can see that the 'this' keyword in JavaScript is assigned to the newly created object and you can try it on an online javascript compiler.
Indirect Invocation
In Javascript, function objects are instances of the Function type. The Function type has two methods: call() and apply(). We can set the value of 'this' keyword in JavaScript while calling the function using these methods.
Code:
function getName(prefix) {
// Here 'this' refers to function object
console.log(prefix + this.name);
}
let john = {
name: 'John'
};
let stacey = {
name: 'Stacey'
};
getName.call(john, "I am ");
getName.call(stacey, "My name is ");
You can also try this code with Online Javascript Compiler
Using arrow functions, the value of this can be set lexically. This means that the arrow function inherits 'this' from the outer functions containing the arrow function.
Code:
let getThis = () => this;
console.log(getThis() === window);
You can also try this code with Online Javascript Compiler
The alert message pops up two times since we have both used call() and apply() methods.
printName(), printName.call(), and printName.apply() functions perform the same action in the example above.
The fundamental purpose of the call() and apply() is to set the context of 'this' inside a function, regardless of whether it is called in the global scope or as an object's method.
3. What is the difference between implicit and explicit 'this' binding?
Ans: Implicit binding occurs when a dot notation is used to invoke the function and whatever is to the left of the dot becomes the context for 'this' in the function.
Explicit binding of 'this' occurs when .call(), .apply(), or .bind() are used on a function.
4. What does 'this' refer to in event handlers?
Ans: In HTML event handlers, 'this' refers to the HTML element that received the event.
Key Takeaways
In this blog, we learned about the behavior of 'this' keyword in JavaScript in different contexts.