Problem Statement
How do arrow functions handle the value of this?
Explanation
Arrow functions do not create their own this; instead, they use the this from the enclosing scope. This makes them great for callbacks, but not suitable as methods or constructors.
Code Solution
SolutionRead Only
const obj = {
name: 'Ava',
regular(){ console.log(this.name); },
arrow: () => console.log(this && this.name)
};
obj.regular(); // 'Ava'
obj.arrow(); // undefined (lexical this from module/global scope)Practice Sets
This question appears in the following practice sets:
