fat-cat

bind

bind() 方法创建一个新的函数,在 bind() 被调用时,这个新函数的 this 被指定为 bind()的第一个参数,而其余参数将作为新函数的参数,供调用时使用

注意点

 Function.prototype._bind = function(thisArg, args){
      if(typeof this !== 'function'){
         throw new TypeError('Bind must be called on a function')
         return
     }

     const me = this

     // new 优先级
     const bindFn = function(){
         me.apply(this instanceof me ? this : thisArg, args.concat(Array.prototype.slice.call(arguments)))
     }

     // 继承原型上的属性和方法
     bindFn.prototype = Object.create(me.prototype)

     return bindFn
 }