Four call-site rules
- new —
thisis the new object. - Explicit —
call/apply/bind. - Implicit — object before the dot:
obj.method(). - Default — bare call:
undefinedin strict mode, global object in sloppy mode.
const obj = {
n: 1,
f() { console.log(this.n); },
};
obj.f(); // 1
const g = obj.f;
g(); // undefined (strict) — lost receiverOpen in VisualizerArrow functions
Arrows do not have their own this. They capture this from the enclosing lexical scope. call/apply cannot rebind it.
const obj = {
n: 1,
f: () => console.log(this && this.n),
};
obj.f(); // not 1 — lexical this from outer scopeOpen in VisualizerMethod extraction pitfalls
Passing obj.method as a callback strips the receiver. Fix with obj.method.bind(obj), an arrow wrapper, or a class field arrow if you accept the tradeoffs.
When not to use this
For plain data transforms, pass arguments explicitly. this shines in object methods and class instance APIs; elsewhere it adds call-site fragility for little gain.