JavaScript Arrow Function Inherits the Parent Scope for "This"
"This" is confusing in JavaScript. It refers to some object. But when "this" refers to an unintended object, our code will act unexpectedly. This happens during the construction of an object, when we want to refer to the own object in its methods. For example,
var name = "global name"
var obj_1 = {
name:'obj name',
call:function(){
console.log(this.name);
}
};
var obj_2 = {
name:'obj name',
call:() => {
console.log(this.name);
}
};
obj_1.call();
//obj name
obj_2.call();
//global name
Here, in obj_1, the method defined by the regular function refers to the own object with "this", whhile the method defined by the arrow function refers to the window object in obj_2
During Object Construction "this" Still Points to The Global Scope However Deep in the Object Structure With Arrow Functions.
Unlike regular function, when an object is constructed, all methods in the object will point "this" to the global obect if we use arrow functions to define object methods..var name = "global name"
var obj_1 = {
name:'obj name',
call:function(){
console.log(this.name);
},
innerObj:{
name:'inner obj name',
call:function(){
console.log(this.name);
}
}
};
var obj_2 = {
name:'obj name',
call:() => {
console.log(this.name);
},
innerObj:{
name:'inner obj name',
call:()=>{
console.log(this.name);
}
}
};
obj_1.innerObj.call();
//inner obj name
obj_2.innerObj.call();
//global name
When do the Arrow Functions Inherits the Parent Scope?
Now the arrow function will inherit the parent scope when a function is defined in an object.
var name = "global name"
var obj_1 = {
name:'obj name',
call:function(){
var func = ()=>{
console.log(this.name);
};
func();
}
};
obj_1.call();
// obj name
While "this" will refer to the global object when a function is defined inside an object mwthod.
var name = "global name"
var obj_1 = {
name:'obj name',
call:function(){
var func = function(){
console.log(this.name);
};
func();
}
};
obj_1.call();
// global name
If the function is defined deep inside the object structure, then the arrow function will help find the closest parent scope while the regure function sill point to the global object.
var name = "global name"
var obj_1 = {
name:"obj name",
call:function(){
var func = () => {
console.log(this.name);
};
func();
},
innerObj:{
name:"inner obj name",
call:function(){
var func = () => {
console.log(this.name);
};
func();
}
}
};
var obj_2 = {
name:"obj name",
call:function(){
var func = function(){
console.log(this.name);
};
func();
},
innerObj:{
name:"inner obj name",
call:function(){
var func = function(){
console.log(this.name);
};
func();
}
}
};
obj_1.innerObj.call();
// inner obj name
obj_2.innerObj.call();
// global name
When is Arrow Function Useful in An Object
The arrow functions can be useful when we want to refer "this" to the own object when we are defining a function inside of the same object.
For example, firing setInterval or setTimeout in an object for a method in the same object.
<button id = "call">Call</button>
<script>
var counter = {
count:0,
start:function(){
setInterval(()=>{this.counting()},200);
},
counting:function(){
couterButton.innerHTML = this.count++;
}
}
</script>
Here the function called in the setInterval function is an defined by an arrow function. It would not work with a regular function, because "this" does not mean the counter object then will not have the counting method.
Comments
Post a Comment