Your understanding of arrow functions is mostly correct: Arrow functions have a lexical this
and its value within the arrow function is determined by the surrounding scope.
You probably assume that this
gets a different value within the person
object literal. This is not the case.
So let's see what this
refers to in your (global) scope:
console.log(this === window); // true
let person = {
name: 'Jim',
test: console.log(this === window), // true
sayName: () => {
console.log(this === window); // true
}
};
person.sayName();
As you can see, when the arrow function assigned to sayName
is created, this
refers to the window
object.