In this post, you will learn about "this" keyword in JavaScript and it's usage.
Precap
What is this keyword in JavaScript?
What is the value of this in JavaScript?
What is the “this” keyword in JavaScript?
JavaScript "this" keyword is a reference to an object that belongs to. The "this" keyword returns value when the function or method is called (in which this is specified).
In other words, “this” is a reference to objects and values.
Example
In the following example, the employee object has four properties: id, name, designation and salary, and emp_data method.
<!DOCTYPE html> <html> <head> <title>this keyword example</title> </head> <body> <h1>Employee Details:</h1> <h3 id="emp"></h3> <script> let employee={ id:123456, name:"divan boze", designation:"Manager", salary:50000, emp_data:function () { return this.id+"<br>"+this.name+"<br>"+this.designation+"<br>"+this.salary+"<br>"; } }; //display data on screen document.getElementById("emp").innerHTML=employee.emp_data(); </script> </body> </html>
this keyword reference to the employee object |
What is the value of “this” in JavaScript?
What is the use of “this” keyword?
Using “this” in a global context
let a=this; document.write(a); //In output [object window]
Example:-
this.j=700; console.log(window.j);//output in console is 700.
when you assign a property to this object, JavaScript will assign it to a global object because this is a reference to a window object.
console.log(this===window);
In the console window, it evaluated true.
Using “this” in the function
//function context function demo() { return this; } console.log(demo()); //Output in console window:-[object Window]
“this” in strict mode
//function context in strict mode function demo() { "use strict"; return this; } console.log(demo());//output:- undefined
Using “this” in the method
<!DOCTYPE html> <html> <head> <title>this in method</title> </head> <body> <h2> select your flowers:</h2> <p id="flw"></p> <p id="flw1"></p> <script> //this in method const flower1={ name:"rose", color:"white", info:function(){ return this.color+" "+this.name; }, }; const flower2={ name:"tulip", color:"red", info:function(){ return this.color+" "+this.name; }, }; document.getElementById("flw").innerHTML=flower1.info(); document.getElementById("flw1").innerHTML=flower2.info(); </script> </body> </html>
Output:-
this keyword in the object method |
When the method is called with its objects the information is displayed correctly in the output.
How to access “this” in JavaScript?
//access global variable var city="newyork"; function findcity() { var city="washington"; document.write("city inside function:-",city,"<br>"); document.write("city outside function:-",city); } findcity();
When variable accessed without “this” keyword both variable values are consider as “washington”.
Accessing global variable without this keyword |
//access global variable using this var city="newyork"; function findcity() { var city="washington"; document.write("city inside function:-",city,"<br>"); document.write("city outside function:-",this.city); } findcity();
When one variable accessed with “this” keyword two different values displayed. Because “this” keyword reference to global variable that is defined outside function.
This post covered basic knowledge about “this” keyword. It usage in function, object and variable in strict mode and non-strict mode.
0 Comments