$("btn").click(function(){
function(){
console.log(this);
}
})
Hi All, where "this" is refering in this piece of code?A interviewer asked me,i got confused.Please suggest.Thanks
$("btn").click(function(){
function(){
console.log(this);
}
})
Hi All, where "this" is refering in this piece of code?A interviewer asked me,i got confused.Please suggest.Thanks
this keyword refers to an object, that object which is executing the current bit of javascript code.
In other words, every javascript function while executing has a reference to its current execution context, called this. Execution context means here is how the function is called.
To understand this keyword, only we need to know how, when and from where the function is called, does not matter how and where function is declared or defined.
function bike() {
console.log(this.name);
}
var name = "Ninja";
var obj1 = { name: "Pulsar", bike: bike };
var obj2 = { name: "Gixxer", bike: bike };
bike(); // "Ninja"
obj1.bike(); // "Pulsar"
obj2.bike(); // "Gixxer"