The ” this
” keyword is a fundamental concept in JavaScript that can be both powerful and confusing. Understanding how this
works is essential for writing flexible and reusable code in JavaScript. However, the behavior of this
can be difficult to predict in certain situations, and incorrect use of this
can lead to bugs and hard-to-debug errors. In this article, we will explore the this
keyword, examining how it works and common use cases.
What is “this” keyword?
In javascript, the “this” keyword is a reference to the object that is executing the current function. The value of “this” is determined by how a function is called. If it’s called as a method inside an object, this will always return a reference to that object; However, If it’s called as a stand a lone object, or outside of an object, this will return the global object which is the Window object in browsers.
Let’s do some examples to better understand more about “this” is dynamically changes based on how the function is called
ex1:
function getName(){
console.log(this)
}
getName();
=> "this" is inside a regular function - not a method of an object, so "this" is referencing the global object. It maybe window, undefined or something else depends on the context
ex2:
const item = {
label: "Banana",
getItem(){
console.log("this", this)
},
};
item.getItem();
=> "this" is inside of a method which is a part of an object item, so this is referencing to the object item.
If a function is a part of an object, we call that function a method, "this"
references that object itself.
ex3:
class Item {
label = "Banana";
getItem(){
console.log("this", this)
}
}
const item = new Item();
item.getItem();
=> class Item has an instance label = "Banana", and a method getItem, so "this" is referencing to the instance. Item {label: "Banana"}
ex4:
class Item {
label = "Banana";
getItem(){
function myFn(){
console.log("this", this);
}
myFn();
}
}
const item = new Item();
item.getItem()
=>"this" is undefined
"this" points to function myFn, which is a regular function that we are calling it directly, so this points to the global object. In ES6, we get undefined since by default "this" will prevent us from accidentally modifying the global object.
To fix this issue, we can use arrow function, like this:
class Item {
label = "Banana";
getItem() {
[1, 2, 3].map((item) => {
console.log(this.label);
});
}
}
const item = new Item();
item.getItem();
The arrow function preserves the “this
” value of the enclosing lexical context (in this case, the Item
object), allowing us to access the label property using this.
label
Quick Notes
- The
this
keyword refers to the current execution context, i.e., the object that is currently being executed. - The value of
this
depends on how a function is called, not where it is defined. - When a function is called as a method of an object,
this
refers to the object itself. - When a function is called as a standalone function,
this
refers to the global object (in non-strict mode) orundefined
(in strict mode). - Arrow functions do not have their own
this
value; instead, they inherit thethis
value of the enclosing lexical context. - In some cases, you may need to use the
bind()
,call()
, orapply()
methods to explicitly set thethis
value for a function. - When using “
this
” inside a callback function, you may need to use a variable to store the value ofthis
outside the function and use that variable inside the function to access the correctthis
value.
Understanding how "
works is an important part of working with JavaScript, especially when writing object-oriented code or using complex function calls.this
"