I am pasting a snippet from mozilla docs.
An arrow function does not create its own this, the this value of the enclosing execution context is used. Thus, in the followi
Where was the function created, not where it was called, that is it's enclosing context
. Your function is created in the function Person
, so this function is the enclosing context for the arrow function
.
You only create an arrow function
and pass it to the setInterval
, it is not created in the setInterval
's definition. This
function Person(){
this.age = 0;
setInterval(() => {
this.age++;
}, 1000);
}
is equivalent to this. Here you see that func
's enclosing context is the Person
.
function Person(){
this.age = 0;
var func = () => {
this.age++;
};
setInterval(func, 1000);
}