Skip to content
Onsite

Function.prototype.bind Polyfill

MediumP1Coding

Implement myBind, a polyfill for Function.prototype.bind.

myBind is called with the target function as this: myBind.call(fn, context, ...presetArgs). It returns a new function that:

  • calls fn with this set to context,
  • passes presetArgs first, then the arguments of the new call,
  • when called with new, ignores context, creates the instance as usual, and the instance still inherits from fn.prototype.

If the target is not a function, throw a TypeError.

Example 1

Input
function greet(g, p) { return g + ', ' + this.name + p; } myBind.call(greet, { name: 'Ann' }, 'Hi')('!')
Output
'Hi, Ann!'

Constraints

  • Do not use Function.prototype.bind.
  • fn.call, fn.apply and Reflect.construct are allowed.

Hints

Run checks the 3 visible tests, Submit all 8.