Sub topic 10
Arrow Functions and Lexical this
This chapter assumes you already know the four call-site rules from earlier chapters: default, implicit, explicit, and new binding. It also assumes you understand closures, because arrow functions use the same mechanism for this.
The Working Model
Arrow functions do not have their own this. When code inside an arrow reads this, JavaScript resolves it like an ordinary variable. It looks outward to the nearest enclosing regular function, or to the top level, and uses that scope's this. This is called lexical this: the value depends on where the arrow is written, not on how it is called.
As a result, none of the call-site rules apply to an arrow:
| Call style | Regular function | Arrow function |
|---|---|---|
| `fn()` | `undefined` (strict) or `globalThis` | Enclosing `this` |
| `obj.fn()` | `obj` | Enclosing `this` |
| `fn.call(x)` / `fn.apply(x)` | `x` | Enclosing `this` (`x` ignored) |
| `fn.bind(x)()` | `x` | Enclosing `this` (`x` ignored) |
| `new fn()` | New object | `TypeError`: not a constructor |
The Classic Fix: Callbacks Inside Methods
Arrows mainly solve the lost-binding problem inside callbacks. Before ES2015, a callback in a method got its own this from however the caller invoked it, so developers wrote const self = this or used .bind(this). An arrow captures the method's this directly:
const timer = {
seconds: 0,
start() {
const id = setInterval(() => {
this.seconds++; // this is timer, captured from start()
console.log(this.seconds);
if (this.seconds === 3) clearInterval(id);
}, 1000);
}
};
timer.start(); // logs 1, 2, 3start() is called as timer.start(), so implicit binding makes its this equal to timer. The arrow has no this of its own, so it uses start's. If the callback were a regular function, setInterval would call it with default binding. You would get globalThis in browsers, a Timeout object in Node, or undefined in strict code, and this.seconds++ would fail.
Arrows Ignore call, apply, and bind
Because the arrow has no this slot, explicit binding has nothing to set. The thisArg is silently ignored. No error is thrown:
const arrow = () => this;
console.log(arrow.call({ a: 1 }) === arrow()); // true
console.log(arrow.bind({ a: 1 })() === arrow()); // trueArguments still pass through normally. Only this is fixed. This matters when a library calls your callback with a specific this, such as jQuery handlers or some test frameworks. An arrow quietly opts out of that value.
When Arrows Are the Wrong Choice
Object literal methods
An object literal does not create a scope. An arrow defined as a property therefore takes this from the code around the object, not from the object itself:
'use strict';
const user = {
name: 'Ada',
greetArrow: () => `Hi, ${this?.name}`,
greetMethod() {
return `Hi, ${this.name}`;
}
};
console.log(user.greetArrow()); // 'Hi, undefined'
console.log(user.greetMethod()); // 'Hi, Ada'Here this is the top-level this: undefined in a module, the module's exports object in CommonJS, or globalThis in a browser script. In none of these cases is it user.
Prototype methods and constructors
Arrows cannot be used with new, and they have no prototype property. Assigning an arrow to Foo.prototype.method gives every instance a method whose this is the scope where the assignment was written. Use method shorthand or regular functions there.
Handlers that need the dynamic this
Some APIs pass useful context through this, such as a DOM listener receiving the element. An arrow discards that value. You can still use an arrow, but read event.currentTarget instead. The final chapter covers this case.
Exercise
What does this code log, and why? const obj = { val: 10, run() { const f = () => this.val; return f.call({ val: 99 }); } }; console.log(obj.run());
Show answer
It logs 10. run is called as obj.run(), so its this is obj. The arrow f takes this lexically from run, and call({ val: 99 }) cannot override an arrow's this, so this.val is obj.val, which is 10.
Exercise
A teammate writes const counter = { n: 0, inc: () => { this.n++; } }; in an ES module, and calling counter.inc() throws. Explain the error and fix it.
Show answer
The arrow is not inside any function, so this is the module's top-level this, which is undefined. Reading this.n then throws a TypeError. The fix is to use method shorthand, inc() { this.n++; }, so that implicit binding sets this to counter.