JS手写代码之call, apply

思路

函数的实现其实都借助了点调用。利用第一个参数做个中转,调用完之后删除。

call

Function.prototype.myCall = function(context = windows, ...args) {
  context._fn = this
  const result = context._fn(...args)
  delete context._fn
  return result
}

apply

Function.prototype.myApply = function(context = windows, args) {
  context._fn = this
  const result = context._fn(args)
  delete context._fn
  return result
}

posted @ 2021-04-14 16:31  有点油  阅读(138)  评论(0)    收藏  举报