Function.prototype.bind Polyfill

FunctionsPolyfillthis binding

Implement Function.prototype.myBind.

Function Signature:

Function.prototype.myBind = function (context, ...boundArgs) { }

Requirements:

  • Return a new function — do not invoke immediately
  • Pre-fill boundArgs (partial application); merge with call-time args
  • When the returned function is called with new, ignore the bound context and use the new instance instead
  • Preserve the prototype chain so instanceof works correctly on new instances

Examples

Example 1
Input
function greet(greeting, punct) { return greeting + ' ' + this.name + punct; }
const greetAlice = greet.myBind({ name: 'Alice' }, 'Hello');
greetAlice('!')
Output
'Hello Alice!'
Explanation
Context is fixed, first arg is pre-filled, second arg is supplied at call time.
Example 2
Input
function Point(x, y) { this.x = x; this.y = y; }
const BoundPoint = Point.myBind(null, 10);
const p = new BoundPoint(20);
console.log(p.x, p.y);
Output
10 20
Explanation
When called with new, the bound null context is ignored; the new instance is used.

Constraints

  • Implement the basic version first, then add the `new` handling

Notes

  • `this instanceof boundFn` is true when called with new — use that to detect the constructor case
  • Set `boundFn.prototype = Object.create(originalFn.prototype)` to preserve the chain

Hints

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