← JavaScript Coding

Polyfill: call( )

medium

Problem statement

Implement Function.prototype.myCall, a small version of JavaScript's built-in call method.

The goal is to run a function with a specific this value and pass arguments one by one.

Requirements

  • fn.myCall(obj, a, b) should behave like fn.call(obj, a, b).
  • Support primitive contexts by boxing them with Object(context).
  • Use globalThis when the context is null or undefined.
  • Do not leave temporary properties on the context object.

How to approach Polyfill: call( )

The strategy an interviewer expects you to reach for.

fn.call(obj, …args) is explicit this control. Interviewers want the method-call trick, not fn.bind. Naive move Assign obj.fn = this; obj.fn(…args) with a string key. You can overwrite obj.fn and leave garbage if the call throws. Where it breaks null/undefined this should become globalThis. Primitives must be boxed (Object(context)). Temporary keys must be cleaned in finally. The fix Use a Symbol key on the target, invoke as a method, delete in finally, forward args with rest/spread. Remember: Symbol + finally: no leftover keys even when the function throws. Say this in the interview: I'd box the context, hang the function on a Symbol key, call it as a method, and always delete that key in finally.

Premium

The full solution is part of HelloFrontend Pro

The question above is free to read in full. Upgrade to unlock the interactive workspace and the senior-level walkthrough that go with it.

  • Runnable editor with the hidden test suite
  • Progressive hints that unlock as you get stuck
  • Senior-level reference solution with a line-by-line walkthrough
Unlock the full solution →

Already a member? Log in to open the workspace.

More JS Coding questions

View all JS Coding →
JS CodingEasy

Polyfill: map( )

JS CodingEasy

Polyfill: Array.prototype.reduce

JS CodingMedium

4. Improve a function

JS CodingMedium

5. Throttle

JS CodingMedium

13. Deep Clone

JS CodingMedium

14. Event Emitter

Explore related prep hubs

  • All JavaScript coding
  • JavaScript interview questions
  • JS polyfill interview questions
  • Debounce & throttle questions
  • React interview hub