FrontendInterviews.dev

Loading problem…

35. Function.prototype.call

Easy•

The Function.prototype.call() method invokes a function immediately with a specified this value and arguments passed individually.

Implement your own Function.prototype.call without using the native call, apply, or bind methods. To avoid overriding the built-in method, implement it as Function.prototype.myCall.

Example

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

const user = { name: "Alice" };

greet.myCall(user, "Hello"); // "Hello, Alice"

Constraints

  • Native prohibition: Do not use `Function.prototype.call`, `apply`, or `bind` internally.
  • Property collision safety: Use a unique `Symbol()` as the temporary property key to prevent overwriting existing keys on the context object.
  • Cleanup reliability: Use a `try...finally` block to guarantee the deletion of the temporary property after the function executes.
  • Result preservation: The return value of the original function must be captured and returned by `myCall`.
Accepted23/26|Acceptance Rate88.5%