bind实现 -- 实现原型上的属性和方法

Function.prototype.myBind = function (thisArg, ...args) {
  var self = this
  console.log(self)
  // new优先级
  var fbound = function (...innerArg) {
      self.apply(this instanceof self ? this : thisArg, args.concat(innerArg))
  }
  // 继承原型上的属性和方法
  console.log(self.prototype)
  fbound.prototype = Object.create(self.prototype);

  return fbound;
}


const obj = { name: '写代码像蔡徐抻' }
function foo() {
    console.log(this)
    console.log([...arguments])
}
foo.prototype.aaa = 1
foo.myBind(obj, 'a', 'b', 'c')(1)  //输出写代码像蔡徐抻 ['a', 'b', 'c'
发布了235 篇原创文章 · 获赞 88 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/qq_34629352/article/details/105459709