const person = {
name: John
print: ()=> {
console.log(this.name)
}
}
person.print()
The above will not print anything, as it'd say 'undefined' doesn't have any property name.
I read somewhere that arrow function use lexical scope to define 'this', and the object person doesn't create any lexical scope, hence 'this' is undefined.
My question is that why an Javascript object doesn't define any lexical scope? I'd have guessed that this refers to the object..
edit: I understand the difference between the normal function and arrow function. This is a very specific questions as in WHY the arrow function doesn't not work in an object due to the lexical scope.
I don't believe this is a dup