Aryan PrajapatKnowledge Contributor
What do you understand by “this” keyword in javascript?
What do you understand by “this” keyword in javascript?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
Ans: In JavaScript “this” keyword is widely used and it points to a specific object that is executing a current piece of code. It refers to the object that is executing the current function. If the function is a regular function, “this” refers to the global object. If the function referenced is a method in an object, “this” refers to the object itself.
Below are the rules that apply to “this” keyword to know which object it is referencing:
Global Scope
If a function is called from a global scope which includes ‘this’ keyword, it will always point to the window object.
For Example:
//global declaration of num having global scope
var num = 150;
function global_this() {
// local variable inside function has local scope
var num = 250;
alert(“mynum = ” + mynum); // answer is 250 called locally
alert(“this.mynum = ” + this.mynum); // answer is 150 as “this” keyword points to //global variable i.e., window object.
}
global_this();
Object’s Method
By using a new keyword if an object is created then “this” will point to that specific object
For example:
// declaring global variables 100 has global scope
var myNum = 100;
function printNum() {
this.myNum = 300;
this.display = function () {
var myNum = 400;
alert(“myNum = ” + myNum); // 400
alert(“this.myNum = ” + this.myNum); // 300
};
}
var objM = new printNum();
objM.display();
“this” keyword inside the display() method of the object ‘objM’ will point to the value outside the scope of the display() method.
call() or apply() method
A function can be invoked using the () operator or using call() and apply the () method also. The main aim of call() and apply() is to set the context of “this” keyword inside a function regardless of whether that function is being called in the global scope or as an object’s method.
bind() method
The bind() method is used to set the context of ‘this’ keyword to a particular object when a function is invoked. It is mostly helpful in placing the context of this for a callback function.
For example:
var myNum = 50;
function test() {
alert(this.myNum);
}
var obj_1 = { myNum : 100 , demo1: test};
var obj_2 = { myNum : 150 , demo1:test };
test(); // ‘this’ will point to the global window object
test.call(obj_1); // ‘this’ will point to obj_1
test.apply(obj_2); // ‘this’ will point to obj_2
obj_1.demo1.call(window); // ‘this’ will point to global window object
test.apply(obj_2); // ‘this’ will point to obj_2
//global scope of variable myNum
var myNum = 200;
function test(call)
{
//local scope of variable myNum
var myNum = 250;
call();
};
var obj1 = {
myVar: 350,
Thisfunc : function() {
alert(“‘this’ points to myNum ” + this + “, myNum = ” + this.myNum);
}
};
test(obj1.Thisfunc); //this points to the global window object
test(obj1.Thisfunc.bind(obj1)); // setting “this” value using bind() method