Skip to the solution
EasyDSA6 min read

Polyfill Function.prototype.call and apply

1.2k views

The problem

Problem, as an interviewer would state it: "Implement Function.prototype.myCall and myApply from scratch — WITHOUT using the real native .call/.apply themselves — correctly binding this and forwarding arguments."

Examples:

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

Clarifying questions expected:

  • Since this can't use real .call/.apply internally, what's the genuine mechanism for actually invoking the function WITH a specific this binding?
  • Does the temporary technique used to achieve this need to avoid leaving any trace (a leaked property) on the real context object afterward?
  • What is the real, correct default this binding if thisArg is null/undefined?

Code / implementation expected: Yes — real, direct proof that this is correctly bound and arguments correctly forwarded, PLUS a real, direct check confirming no property leaks onto the context object afterward.

  • call
  • apply
  • polyfill

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 · 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: call/apply polyfills correctly bind this and forward arguments, and a real check confirms no property leaks onto the context object afterward

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 19 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 nextEasy

Array.prototype.forEach Polyfill

Continue