Learn JavaScript
❯ objects.this_deep

this Binding Rules, Fully in JavaScript

$concept

this binds by call shape, completely: method calls (obj.f()) bind the receiver; bare calls bind undefined (strict) or global (sloppy); new binds the fresh instance; call/apply/bind set it explicitly; arrows ignore all of it (lexical). Four-plus-one rules predict every case β€” this confusion is always a call-shape question in disguise.

Callbacks lose receivers (setTimeout(obj.method) detaches), classes in strict mode crash loudly on detachment (better than silent globals), and bound copies (method.bind(this) in constructors) freeze receivers for event systems. Master the five shapes and this becomes deterministic.

$how it works
1

Method calls bind receivers

obj.f() sets this to obj β€” the only rule most code ever needs.

2

Bare calls bind nothing useful

f() gives undefined (strict) β€” detached methods and callbacks land here by accident.

3

new binds fresh instances

new F() sets this to the new object β€” constructors rely on exactly this rule.

4

Arrows opt out entirely

Arrow this is lexical, immune to call shape β€” callbacks preserving outer receivers, methods breaking when misused.

$example
let obj = {
name: "Ada",
hi() { return "hi " + this.name; },
later: null,
};
obj.later = obj.hi;
console.log(obj.hi());
try {
let f = obj.hi;
f();
} catch (e) {
console.log("detached:", e instanceof TypeError);
}
Output
hi Ada
detached: true
1Method call binds obj β€” hi Ada prints (modules are strict, but bound correctly here).
2Detached f() binds undefined; reading .name of undefined throws TypeError.
3Call shape decided both outcomes β€” same function, different this.
$use cases
Diagnosing every this bug as a call-shape question
Binding callbacks (bind, arrows) before handing to event systems
Choosing method vs arrow per dynamic-vs-lexical need
Reading strict-mode detachment as loud, correct failure

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself β€” building beats watching.