JavaScript Arrow Functions and Lexical this
Quick answer
An arrow function has no independent this binding. References to this resolve through its surrounding lexical environment.
How it works
An arrow inside an ordinary method can read that method call’s receiver after the method returns. A property holding an arrow does not supply the arrow with a new this. Explicit receiver APIs such as call and bind, introduced next, cannot replace the arrow’s lexical this.
const box = {n: 7, make() { return () => this.n; }};const read = box.make(); console.log(read());Expected output in Node.js:
7The arrow reads this from the surrounding method invocation.
function make() { return {n: 99, read: () => this.n}; }const outer = {n: 3, make}; const inner = outer.make();console.log(inner.read());Expected output in Node.js:
3The property call cannot replace the captured receiver.
Common mistakes
Avoid arrow syntax for a method that needs a dynamic receiver. Use an ordinary method instead.
Practice
Change the second example’s arrow to an ordinary method and compare.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Arrow Functions and Lexical this — Knowledge Check
Apply the taught concept and predict the output.
Where does an arrow resolve this?
Which callable is appropriate for a dynamic method receiver?
What can a returned arrow inside a method retain?
What is the exact console output in JavaScript Arrow Functions and Lexical this example 1?
const box = {n: 7, make() { return () => this.n; }};const read = box.make(); console.log(read());What is the exact console output in JavaScript Arrow Functions and Lexical this example 2?
function make() { return {n: 99, read: () => this.n}; }const outer = {n: 3, make}; const inner = outer.make();console.log(inner.read());