I thought I understood the relationship between this and arrow functions but the following snippet is making me question my understanding.
let person = { name: 'Jim', sayName: () => { console.log(this.name); } }; person.sayName(); I understand that arrow functions capture the this value of the enclosing context. I was expecting this would be the object but instead it is Window.
Can someone help me understand why this is the case?
thiswill be undefined if you want to get the value of name either use the normal function syntax or reference the object name so it'sperson.nameObjectliteral doesn't create an enclosing context. Only the body of a normal function does.