call/apply/bind

// func.geCall(obj, 'arg1', 'arg2')
Function.prototype.geCall = function (context) {
  var context = context || window
  // 给 context 添加一个函数
  context.fn = this
  // 将 context 后面的参数取出来
  var args = [...arguments].slice(1)
  var result = context.fn(...args)
  // 删除 fn
  delete context.fn
  return result
}

// func.geApply(obj, ['arg1', 'arg2'])
Function.prototype.geApply = function (context) {
  var context = context || window
  context.fn = this

  var result

  if (arguments[1]) {
    result = context.fn(...arguments[1])
  } else {
    result = context.fn()
  }

  delete context.fn
  return result
}

Function.prototype.geBind = function(context){
  let _this = this
  let args = [...arguments].slice(1)
  const fn = function() {
    return _this.apply(context, [...args, ...arguments])
  }
  return fn
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容