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
42
Explanation
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
84
Explanation
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

Read the full write-up for Function.prototype.call Polyfill
</>JavaScript
Loading editor…
Test Result
Run your code, or Submit to test it