实现call、apply、bind方法

  Function.prototype.callPolyfill= function(){
  // 获取参数列表
  let args = [...arguments]
  // 获取要绑定的that
  let that = args[0] || window
  // 将原函数复制绑定当前的that
  that.fn = this
  // 执行复制绑定that后的函数
  const result = that.fn(...args.slice(1))
  // 解绑,防止破环that
  delete that.fn
  // 返回结果
  return result
}
Function.prototype.applyPolyfill = function(){
  // 获取参数列表
  let args = [...arguments]
  // 获取要绑定的that
  let that = args[0] || window
  // 将原函数复制绑定当前的that
  that.fn = this
  // 执行复制绑定that后的函数
  args[1] = args[1] || []
  const result = that.fn(...args[1])
  // 解绑,防止破环that
  delete that.fn
  // 返回结果
  return result
}
Function.prototype.bindPolyfill= function(){
  // 获取参数列表
  let args = [...arguments]
  // 获取要绑定的that
  let that = args[0] || window
  let targetThis = this
  return function(){
    // 将原函数复制绑定到当前的that
    that.fn = targetThis
    const result = that.fn(...args.slice(1),...arguments)
    // 解绑,防止破环that
    delete that.fn
    // 返回结果
    return result
  }
}

你可能感兴趣的:(实现call、apply、bind方法)