Interview Q&A · JavaScript · 8 questions in this topic
`this` Binding Rules — JavaScript Interview Q&A
The four (plus one) binding rules — implicit, explicit, new, default, and lexical. Part of Objects, Prototypes & this · 204 questions in the JavaScript bank.
How is `this` determined (the four binding rules)?
Answer
Call-site rules in precedence order: new (fresh object), explicit (call/apply/bind), implicit (owning object: o.f()), default (undefined in strict/modules, global in sloppy). Arrows skip all four — lexical from enclosing scope.
New > explicit > implicit > default, arrows opt out entirely.
Key points
- 1new — fresh instance.
- 2explicit — call/apply/bind argument.
- 3implicit — receiver object.
- 4default — undefined (strict) / global (sloppy).
Example
function f() { return this; } f(); // undefined (strict) — default binding ({ m: f }).m(); // the object — implicit binding
💡 Recite in precedence order — interviewers grade the ordering.
Why does `this` get lost in callbacks (and how do you fix it)?
Answer
Passing obj.method detaches the function — the callback invokes it bare (default binding), losing the receiver. Fixes: arrow wrapper (() => obj.method()), bind at registration, or pass receiver where APIs accept it (forEach's thisArg).
Detachment-loses-receiver is the bug shape behind every lost-this.
Example
class UI { constructor() { this.n = 0; } attach(btn) { btn.addEventListener('click', () => this.tick()); // arrow keeps UI } tick() { this.n++; } }
💡 Arrow-at-registration is the standard fix.
What does `new` actually do (four steps)?
Answer
Creates a fresh object linked to the constructor's prototype, calls the constructor with this bound to it, and returns this (unless the constructor returns its own object). new-less calls on constructors misbind this (strict: TypeError fast-fail).
Fresh-link-call-return is the four-step mantra.
Key points
- 1Create — fresh object, prototype linked.
- 2Bind — this is the new object.
- 3Return — this unless object returned.
Example
function P(n) { this.n = n; } const p = new P(1); // p.__proto__ === P.prototype
What is `this` in strict mode vs sloppy mode?
Answer
Strict (and modules, always strict): bare-call this is undefined — fast loud failure. Sloppy: boxed global object (globalThis) — silent global clobbering. Strict also forbids this-mutation and with-statement confusion.
Undefined-beats-global is why strict is the default expectation.
Example
function f() { return this; } // sloppy: globalThis; strict: undefined
💡 Modules-are-strict is the corollary to state.
How do the binding rules interact (precedence puzzles)?
Answer
new beats explicit (bound constructors still allocate fresh), explicit beats implicit (bound method called as o.m uses bound this), implicit beats default. Bound-then-new is the classic puzzle: new wins for this, bound args still prepend.
Work precedence examples, don't just list rules.
Example
function F(x, y) { this.v = [x, y]; } const B = F.bind({ tag: 1 }, 'a'); new B('b'); // this = fresh (new wins), args ['a','b'] (bound kept)
💡 Bound-args-survive-new is the puzzle detail.
How does `this` work in classes (methods, statics, fields)?
Answer
Instance methods: receiver-bound at call (detachment loses it — bind in ctor or arrow fields). Statics: this is the class (inheritable statics see subclasses). Arrow fields: per-instance lexical this (memory cost per instance).
Bind-or-arrow-field is the framework-callback decision.
Example
class C { handler = () => { /* lexical this, per instance */ }; method() { /* dynamic this */ } }
What is `globalThis` and why does it exist?
Answer
The universal global reference across realms (window, global, self, worker scopes) — portable global access without environment sniffing. Use for feature detection and genuine globals; never as a variable dump (modules scope properly).
One name for every realm is the portability win.
Example
// portable env check: // typeof globalThis.fetch === 'function'
How do you debug `this` surprises quickly?
Answer
Log this at the call (console.log(this)), check strictness, trace detachment (passed bare?), and verify arrow-vs-regular at definition. The checklist resolves 95%: strict? detached? arrow-method? bound-lost?
Definition-form plus call-shape is the two-spot inspection.
Example
// debug print at suspect call: // someHandler = someHandler.bind(this); // fix once diagnosed
💡 A four-item checklist beats re-reading binding theory mid-incident.
Recommended resourcesAffiliate links · may earn us a commission