Function.prototype.call Polyfill
this bindingPolyfillFunctions
Implement a polyfill for Function.prototype.call named myCall.
Function Signature:
Function.prototype.myCall = function (thisArg, ...argArray) { // Your implementation };
Examples
Example 1
Input
function multiplyAge(multiplier = 1) {
return this.age * multiplier;
}
const mary = { age: 21 };
multiplyAge.myCall(mary);
multiplyAge.myCall(mary, 2);Output
21
42Explanation
The function is called with `mary` as the `this` context. `this.age` refers to `mary.age` which is 21. When multiplier is 2, the result is 42.
Example 2
Input
function multiplyAge(multiplier = 1) {
return this.age * multiplier;
}
const john = { age: 42 };
multiplyAge.myCall(john);
multiplyAge.myCall(john, 2);Output
42
84Explanation
The same function works with different objects. `this` correctly refers to `john` in both calls.
Constraints
- The function should work with any number of arguments
- Primitive `thisArg` values should be boxed to objects
- `null` and `undefined` should default to the global object
- Must not mutate user properties on the `thisArg` object
Notes
- Use a Symbol or a unique string key to avoid property collisions
- Consider using `Object()` constructor to box primitives
- Make sure to delete temporary properties after the function call
- Handle edge cases like calling on non-functions
Hints
Editorial: Function.prototype.call Polyfill
Implementing a Function.prototype.call Polyfill
The real call:
- invokes a function immediately,
- lets you choose the
thisvalue, - forwards arguments in order,
- throws if called on a non-function.
Requirements:
- Validate that "this" is a function, otherwise throw a TypeError
- "thisArg" should default to the global object when null/undefined
- Support primitive thisArg values by boxing them (Number, String, Boolean)
- Do not leak temp properties - clean up before returning
- Return the invoked function's return value
- Preserve argument order and length
- Avoid property name collisions when attaching the temp function
Implementation
// call polyfill Function.prototype.myCall = function (thisArg, ...argArray) { if (typeof this !== "function") { throw new TypeError("myCall must be called on a function"); } // default to global object when null/undefined, box primitives const context = thisArg === null || thisArg === undefined ? globalThis : Object(thisArg); // unique key to avoid overwriting existing properties const fnKey = Symbol("fn"); context[fnKey] = this; const result = context[fnKey](...argArray); delete context[fnKey]; return result; };
Edge Cases Covered
- Non-function
this-> throwsTypeError. null/undefined-> safely usesglobalThis.- Primitive contexts -> boxed via
Object(...). - Temp property collisions -> avoided with
Symbol. - Return value bubbles back just like native
call.
Complexity
- Time: O(n) where n is argument count (spread).
- Space: O(1) extra (single symbol reference).
</>JavaScript
Loading editor…
Test Result
Run your code, or Submit to test it