跳至主要內容
call 、apply 和 bind 的模拟实现

call

Function.prototype.myCall = function(context = window, ...args) {
  if (this === Function.prototype) {
    return undefined  // 用于防止 Function.prototype.myCall() 直接调用
  }
  context = context || window // 用于防止传入 null
  const fn = Symbol()
  context[fn] = this
  const result = context[fn](...args)
  delete context[fn]
  return result
}

fxss原创大约 1 分钟javascriptcallapplybind