Problem
Implement myCall(fn, thisArg, ...args) — a standalone version of Function.prototype.call.
call lets you borrow a function and run it with a this of your choosing, passing arguments individually. This is how you reuse methods across unrelated objects (e.g. running an array method on an array-like arguments object).
Your myCall(fn, thisArg, ...args) must:
- Invoke
fnwith itsthisset tothisArg. - Forward the remaining
argstofnindividually. - Return whatever
fnreturns. - Not permanently mutate
thisArg(clean up any temporary property you add).
Input
function greet(greeting) {
return `${greeting}, ${this.name}`;
}
myCall(greet, { name: "Alice" }, "Hi");
Expected output
"Hi, Alice"
Implement from scratch (don't use the built-in call/apply/bind):
function myCall(fn, thisArg, ...args) {
// Your code here
}