SOURCE

function testBind () {
    console.log(this.a) 
}

testBind.prototype.myName = 'testBind'

let obj1 = { a: 1 }

Function.prototype.myBind = function(objThis, ...params) {
  const thisFn = this;//存储调用函数,以及上方的params(函数参数)
  //对返回的函数 secondParams 二次传参
  let funcForBind = function(...secondParams) {
    //检查this是否是funcForBind的实例?也就是检查funcForBind是否通过new调用
    const isNew = this instanceof funcForBind;

    //new调用就绑定到this上,否则就绑定到传入的objThis上
    const thisArg = isNew ? this : Object(objThis);

    //用call执行调用函数,绑定this的指向,并传递参数。返回执行结果
    return thisFn.call(thisArg, ...params, ...secondParams);
  };

  //复制调用函数的prototype给funcForBind
  funcForBind.prototype = Object.create(thisFn.prototype);
  return funcForBind;//返回拷贝的函数
};

let bindThis = testBind.bind(obj1)

let myBindThis = testBind.myBind(obj1)

let sonTestBind = new testBind()

console.log(sonTestBind.myName)

let sonTestMyBind = new myBindThis()

console.log(sonTestMyBind.myName)





console 命令行工具 X clear

                    
>
console