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
fnwiththisset tocontext, - passes
presetArgsfirst, then the arguments of the new call, - when called with
new, ignorescontext, creates the instance as usual, and the instance still inherits fromfn.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
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 8. ⌘Enter runs, ⌘⇧Enter submits.