Skip to the solution
MediumDSA7 min read

Polyfill Function.prototype.bind (with new support)

188 views

The problem

Problem, as an interviewer would state it: "Implement Function.prototype.myBind from scratch — matching real native bind's own documented contract, including its genuinely unusual behavior when the returned function is used as a CONSTRUCTOR."

Examples:

Code
function greet(greeting) { return greeting + ", " + this.name; }
const g = greet.myBind({ name: "Ada" }, "Hello");
g(); // "Hello, Ada"

Clarifying questions expected:

  • Unlike call/apply, bind does not invoke the function immediately — does it need to return a genuinely NEW function instead?
  • Does bind need to support partial application (pre-filling some arguments, with more supplied later at call time)?
  • What happens if the bound function is later called with new — does the pre-bound this still apply?

Code / implementation expected: Yes — real, direct proof matching real native bind for a normal call, PLUS real, direct proof that a bound function used as a constructor correctly ignores the bound this.

  • polyfill
  • bind
  • this

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

7 min read · 9 sections

Run the code

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

JavaScriptReal, direct proof: the bind polyfill matches real native bind for a normal call, and correctly ignores the bound this when used as a constructor via new

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 122 of 154 in JavaScript coding

Keep your momentum going.

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

Up nextMedium

Implement lodash.get(obj, path, defaultValue)

Continue