Polyfill: call( )
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 likefn.call(obj, a, b).- Support primitive contexts by boxing them with
Object(context). - Use
globalThiswhen the context isnullorundefined. - 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.
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
Already a member? Log in to open the workspace.