Question presented to candidate: "Why do call, apply, and bind exist at all — what real problem in JavaScript do they solve, and can you show a concrete situation where you'd actually need one of them?"
What a strong answer should cover:
- 📌 Interview term: the real problem they solve — a regular function's
thisis determined by how it is called (its call-site), not where it is defined — this means passing a method as a plain callback (an event handler, a timer, an array-callback) genuinely detaches it from its original object, breakingthis.call/apply/bindexist specifically to let a developer explicitly, deliberately controlthis, overriding the default call-site rule. - 📌 Interview term: the real, concrete demonstration — verified directly: extracting a method from an object and calling it as a bare, detached function genuinely loses its original
this(calling it directly throws/producesundefinedaccess, depending on strict mode); re-attaching the correctthisvia.bind(originalObject)genuinely fixes it, producing the identical correct result the method gave when called normally. - A precise answer names the shared purpose across all three (deliberate
thiscontrol), while naming their distinct timing:call/applyapply that control for one single, immediate invocation;bindapplies it permanently to a new, reusable function. - 📌 Interview term: a real, common use case — passing an object method as a callback (e.g.
element.addEventListener("click", obj.handleClick.bind(obj))) is one of the single most common real reasonsbindshows up in application code, precisely because event listener callbacks are always invoked withthisdetermined by the LISTENER's own call-site convention, not the original object. - A precise answer names that in modern class-based/arrow-function-heavy code, the NEED for explicit binding has genuinely decreased (class fields with arrow functions capture
thislexically at definition time), but understanding why binding is needed at all remains foundational to understandingthisitself.
Clarifying questions expected:
- None — this is a definitional/technical question; explaining the ROOT problem (call-site-determined
this) rather than just listing method signatures is the strong signal.
Code / implementation expected: Yes — demonstrating a method genuinely losing its this when detached, then fixing it with bind, is the clearest, most convincing proof of understanding the actual purpose.