JavaScript - this Keyword
JavaScript - this Keyword
What is βthisβ keyword?
In JavaScript, the βthisβ keyword contains the reference to the object. It represents the context of the function or current code. It is used to access the properties and methods of the current object.
When this keyword is used inside a function, the this will refer to the object that the function is called with.
In JavaScript, functions are also objects. So, you can use the βthisβ keyword with functions also.
Which object does the βthisβ refer to?
The object referred by the βthisβ keyword depends on how you have used the βthisβ keyword.
For example,
- The βthisβ keyword refers to the window object in the global scope.
- When you use the βthisβ keyword inside the function, it also represents the βwindowβ object.
- The βthisβ keyword refers to an undefined in the strict mode in the function.
- The βthisβ keyword refers to the object when you use it in the object method.
- In the event handler, the βthisβ keyword refers to the element on which the event is executed.
- The βthisβ keyword in methods like call(), apply(), and bind() can refer to different objects.
Syntax
Follow the syntax below to use the βthisβ keyword in JavaScript &minus
this.property
OR
this.method();You can access the properties and execute the objectβs methods using the βthisβ keyword.
JavaScript βthisβ in the Global Scope
When you use the βthisβ keyword in the global scope, it represents the global (window) object. You can access the global variables using the βthisβ keyword in the global scope.
Example
In the below code, we have defined the βnumβ variable and printNum() function in the global scope. After that, we used the βthisβ keyword to access the global variable and functions.
Output
Inside the function: 10
Outside the function: 10JavaScript βthisβ in a Function
When you use the βthisβ keyword in the function, it represents the global scope or βwindowβ object.
Example
In the below code, we use the βthisβ keyword inside the function. You can observe that we access the global variables using the βthisβ keyword inside the function.
Output
The messsage is: Hello World!βthisβ in a Function in Strict Mode
When you use the βthisβ keyword inside the function in the strict mode, it doesnβt refer to any object. The value of the βthisβ keyword becomes undefined.
Example
In the below code, we use the βthisβ keyword inside the function in the strict mode. It prints undefined.
Output
The value of this in the strict mode is: undefinedβthisβ in a Constructor Function
When you use the function as a constructor function to create an object, the βthisβ keyword refers to the object.
Example
We have defined the Animal() constructor function in the code below. We used the βthisβ keyword inside the constructor function to initialize the properties of the object.
Output
Animal Name: Lion
Animal Age: 3
Animal Color: Brownβthisβ in an Arrow Function
When you use the βthisβ keyword in the arrow function, it refers to the scope of its parent object.
For example, when you define the arrow function inside the object method and use the βthisβ keyword inside that, it presents the object. The βthisβ keyword refers to the global object if you define the arrow function inside another function.
Example
In the below code, we have defined the arrow function inside the getDetails() method of the object. When we print the value of the βthisβ keyword, it prints the object.
Output
Value of 'this' inside the getDetails() method: {"size":"10","color":"blue"}
Value of 'this' inside the getInnerDetails() method: {"size":"10","color":"blue"}βthisβ in the Object Method
When you use the βthisβ keyword inside the object method, it represents the object itself.
Example
In the below code, we have defined the βfruit; object. The object contains the printFruitDetails() method, and in the method, we used the βthisβ keyword to access the properties of the object.
Output
Furit Name = Apple
Fruit Color = redβthisβ in a Child Function of an Object Method
When you define the function inside the object method and use the βthisβ keyword inside the function, it represents the global object rather than an object.
Example:
In the below code, we have defined the person object. The person object contains the printDetails() method. In the printDetails() method, we have defined the printThis() function.
In the printThis() function, we print the value of the βthisβ keyword, and it prints the global object.
Output
Inside the printThis() function, Value of 'this' = [object Window]JavaScript βthisβ in Event Handlers
Using the βthisβ keyword with event handlers refers to the HTML element on which the event is executed.
Example
In the below code, we have added the onClick event handler into the <div> element. When users click the div element, we hide the div element using the βdisplayβ property.
Explicit Function Binding in JavaScript
In JavaScript, call(), apply(), or bind() methods are used for the explicit binding.
The explicit binding allows you to borrow the method of the particular object. Using these methods, you can explicitly define the context of the βthisβ keyword.
Letβs understand explicit binding using the examples below.
Example: Using the call() method
In the below code, the lion object contains the color and age property. It also contains the printDetails() method and prints the details using the βthisβ keyword.
The tiger object contains only color and age properties. We used the call() method to call the printDetails() method of the lion object with the context of the tiger object. So, the method prints the details of the tiger in the output.
Output
Color: Orange
Age: 15Example: Using the bind() method
The below code also contains the lion and tiger objects. After that, we used the bind() method to bind the printDetails() method of the lion object into the tiger object.
After that, we use the tigerDetails() method to print the details of the tiger object.
Output
Color: Orange
Age: 15JavaScript βthisβ Precedence
You should use the below precedence order to determine the context of the βthisβ keyword.
-
- bind() method
-
- call and apply() methods
-
- Object method
-
- Global scope