Function.prototype.apply Polyfill

FunctionsPolyfillthis binding

Implement Function.prototype.myApply.

Function Signature:

Function.prototype.myApply = function (context, args) { }

Requirements:

  • Same context handling as call: null/undefined → globalThis, primitives → Object()
  • args is a single array (or array-like) of arguments — spread it into the call
  • If args is omitted or not an array, call the function with no arguments
  • Use a Symbol key, clean up after invocation, return the result

Examples

Example 1
Input
function sum(a, b, c) { return a + b + c; }
sum.myApply(null, [1, 2, 3])
Output
6
Explanation
The array [1, 2, 3] is spread as individual arguments.
Example 2
Input
Math.max.myApply(null, [3, 1, 4, 1, 5])
Output
5
Explanation
Classic use case: spread an array into a variadic function.

Constraints

  • Check `Array.isArray(args)` before spreading — say so if you skip array-like support

Hints

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