AniUI Academy
medium+250 XPPractice

Implement Function.prototype.call yourself

this in JavaScript is not a property of a function, it is a property of the *call*: whatever sits to the left of the dot becomes the receiver. Once that clicks, invoking a function with an arbitrary receiver needs no special machinery at all — you can put the function on the object temporarily and call it through the object.

Implement myCall(fn, thisArg, ...args). It invokes fn with this set to thisArg and with args as its arguments, and returns whatever fn returns. You may assume thisArg is always an object. Your implementation must not use Function.prototype.call, apply or bind: one of the tests replaces all three with functions that throw before calling myCall. It must also leave thisArg exactly as it found it — after myCall returns, thisArg has no extra own property, neither a string-keyed one nor a symbol-keyed one.

What it has to do

  • Invoke fn with this set to thisArg.
  • Forward args as the function's arguments and return its return value.
  • Work when Function.prototype.call, apply and bind have been replaced with throwing stubs.
  • Leave no extra own property behind on thisArg, including symbol-keyed ones.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.