FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Function Bind

10mins

Implement a function myBind(thisArg, ...args) that works like Function.prototype.bind.
It should return a new function with the this keyword set to the provided thisArg and optionally prepended arguments.

Arguments

  • thisArg: The value to be set as this in the function.
  • args: Arguments to prepend to the function call.

Example

function greet(greeting, name) { return greeting + ', ' + name + '!'; } const sayHelloToAlice = greet.myBind({}, 'Hello', 'Alice'); sayHelloToAlice(); // 'Hello, Alice!' const obj = { x: 10 }; function add(a, b) { return this.x + a + b; } const boundAdd = add.myBind(obj, 5); boundAdd(5); // 20

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests