Skip to the solution
MediumFrontend6 min read

What is the purpose of the bind, call, and apply methods?

573 views

The problem

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 this is 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, breaking this. call/apply/bind exist specifically to let a developer explicitly, deliberately control this, 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/produces undefined access, depending on strict mode); re-attaching the correct this via .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 this control), while naming their distinct timing: call/apply apply that control for one single, immediate invocation; bind applies 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 reasons bind shows up in application code, precisely because event listener callbacks are always invoked with this determined 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 this lexically at definition time), but understanding why binding is needed at all remains foundational to understanding this itself.

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.

  • this
  • functions
  • context
  • bind

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: a detached method genuinely loses its this and throws, and .bind() genuinely fixes it

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 81 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What does setTimeout with 0 delay actually do?

Continue