bind

文章链接

      Function.prototype.myBind = function (context) {
        if (typeof this !== 'function') {
          throw new Error('Function.prototype.myBind - what is trying to be bound is not callable')
        }

        var self = this
        var args = Array.prototype.slice.call(arguments, 1)
        var fNull = function () {}

        var fBound = function () {
          var bindArgs = Array.prototype.slice.call(arguments)
          // 当作为构造函数时,this 指向实例,此时结果为 true,将绑定函数的 this 指向该实例,可以让实例获得来自绑定函数的值
          // 如果改成 `this instanceof fBound ? null : context`,实例只是一个空对象,将 null 改成 this ,实例会具有 habit 属性
          // 当作为普通函数时,this 指向 window,此时结果为 false,将绑定函数的 this 指向 context
          return self.apply(this instanceof fBound ? this : context, args.concat(bindArgs))
        }
        // 修改返回函数的 prototype 为绑定函数的 prototype,实例就可以继承绑定函数的原型中的值
        // 使用 fNull函数中转,避免直接修改绑定函数的 prototype
        fNull.prototype = this.prototype
        fBound.prototype = new fNull()
        return fBound
      }
    
      var value = 'window value'
      var foo = {
        value: 'foo value',
      }

      function bar(name, age) {
        this.habit = 'cf'
        console.log(this.value)
        console.log(name)
        console.log(age)
      }

      bar.prototype.friend = 'gaogao'
      var bindFoo = bar.bind(foo, 'xueyue')

      var obj = new bindFoo('18')
      console.log(obj.habit)
      console.log(obj.friend)