FrontendInterviews.dev

Loading problem…

35. Function.prototype.call

Easy•
Acceptance: 90.48%
•
🔓3/3 Pro unlocks today

Implement your own version of Function.prototype.call method. The call method calls a function with a given this value and arguments provided individually.

Requirements

1. Basic Functionality

  • Accept a thisArg parameter to be used as the this value
  • Accept individual arguments (not an array)
  • Call the function with the specified this context
  • Return the result of the function call

2. Edge Cases

  • Handle null or undefined as thisArg (should default to global object)
  • Support functions with any number of arguments (including zero)
  • Work with both function declarations and arrow functions (where applicable)

Example Usage

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

const person = { name: 'Alice' };

// Using built-in call
greet.call(person, 'Hello', '!'); // "Hello, Alice!"

// Using your implementation
greet.myCall(person, 'Hello', '!'); // "Hello, Alice!"

Constraints

  • Must work with any function (except arrow functions as context)
  • Handle edge cases: null/undefined thisArg
  • Should not use Function.prototype.call or apply internally
  • Arguments must be passed individually, not as an array
  • Return value should match the original function's return value