bind的模拟实现

bind的模拟实现如下:

Function.prototype.myBind() = function (context = window, ...args1) {
  if(this === Function.prototype) {
    throw 'Error'
  }
  
  const _this = this
  
  return function F (...args2) {
    if ( this.instanceof F ) {
      return new _this(...args1, ...args2)
    }
    return _this.apply(context, args1.concat(args2))
  }
}

猜你喜欢

转载自www.cnblogs.com/izyk/p/11447294.html