polyfill for call 1

polyfill for call

Function.prototype.myCall = function (...args) {
  let obj = args[0]
  const params = args.slice(1)

  obj = {
    ...obj,
    fn: this
  }

  return obj.fn(...params)
}

Here is what the above code is Doing:
1. We’re creating a new object and assigning it to the first argument passed to myCall.
2. We’re adding a new property to the object called fn and assigning it to the function that myCall was called on.
3. We’re invoking the function and passing in the remaining arguments.

Similar Posts