FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Function Apply

10mins

Implement a function myApply(fn, thisArg, args) that calls the given function fn with a specified this value and arguments provided as an array.

Arguments

  • fn: Function to be called
  • thisArg: The value of this inside the function
  • args: An array of arguments to pass to the function

Example

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

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests