Pellentesque nec neque ex. Aliquam at quam vitae lacus convallis pulvinar. Mauris vitae ullamcorper lacus. Cras nisi dui, faucibus non dolor quis, volutpat euismod massa. Donec et pulvinar erat.
Arrow function does not create its own this context. Instead, it binds this keyword lexically which is a fancy way of saying that it takes the meaning of this from the enclosing context. In ES5 we can either use Function.prototype.bind to grab the this value from another scope to change a function’s execution context or assign the value of this to another variable (var that = this).
In this code caller of setTimeout is window object. But inside setTimeout, this refers to its current surrounding scope and no further. Thus the inner function knew to bind to the inner function only, and not to the object’s method or the object itself.
Pellentesque nec neque ex. Aliquam at quam vitae lacus convallis pulvinar. Mauris vitae ullamcorper lacus. Cras nisi dui, faucibus non dolor quis, volutpat euismod massa. Donec et pulvinar erat.
Arrow function does not create its own this context. Instead, it binds this keyword lexically which is a fancy way of saying that it takes the meaning of this from the enclosing context.
I think, by using lexical scoping, arrow function solves the confusion around
this
to an extent.@jishvi Can you elaborate?
Arrow function does not create its own
this
context. Instead, it bindsthis
keyword lexically which is a fancy way of saying that it takes the meaning of this from the enclosing context. In ES5 we can either useFunction.prototype.bind
to grab thethis
value from another scope to change a function’s execution context or assign the value of this to another variable (var that = this
).In this code caller of
setTimeout
iswindow
object. But insidesetTimeout
,this
refers to its current surrounding scope and no further. Thus the inner function knew to bind to the inner function only, and not to the object’s method or the object itself.That is precisely what I said.
Yurp, ure right. Thanks alot for the additional information