FrontendInterviews.dev

Loading problem…

39. Function.prototype.bind

Medium•

The Function.prototype.bind() method returns a new function with a fixed this value and optionally pre-filled arguments.

Implement your own Function.prototype.bind without using the native bind method. To avoid overriding the built-in method, implement it as Function.prototype.myBind.

Example

function greet(greeting) {
  return `${greeting}, ${this.name}`;
}

const user = { name: "Alice" };

const sayHello = greet.myBind(user, "Hello");
sayHello(); // "Hello, Alice"

Constraints

  • Native prohibition: You may not use the built-in `Function.prototype.bind`.
  • Result type: Must return a new function, not a wrapper object.
  • Constructor priority: Must detect and support `new` operator calls using `instanceof` or `new.target`.
  • Argument order: Bound arguments must pre-empt call-time arguments.
Accepted14/31|Acceptance Rate45.2%