This question already has an answer here:
- How does the “this” keyword work? 22 answers
I very confusing about the value of this
at the moment of an invocation function using arrow functions. Let's take my example:
var obj = {
property: 5,
func1: function () {
console.log(this.property);
},
func2: () => {
console.log(this.property);
}
}
When i put this code on the browser console, an interesting thing happens, func1()
will output 5
as expected *, but when i run func2
i got undefined
. What is going on here? Why the value of this
inside func2
makes reference to the global object(Window
in this case).
I think i expect that output, because it is how it works, thats the reason why Alan and slevetman explains here and here respectively. But according to the Jason's explanation
Arrow functions do not have their own this value. The value of this inside an arrow function is always inherited from the enclosing scope.
So, why the value of this inside func2
is not inheriting the value of his enclosing scope obj
?