scofieldfan / turtle-rock

turtle-rock js 面试高频
210 stars 38 forks source link

实现 function 的 bind 函数中,求教instanceof这句是什么意思? #4

Open Davidyanlong opened 5 years ago

Davidyanlong commented 5 years ago
     Function.prototype.mybind = function(context, ...args) {
            let fun = this;
            function bound(...args2) {
                     //如题,这句话不理解是什么意思?什么情况下  this instanceof bound ==true
                      let self = this instanceof bound ? this : context;

                        return fun.apply(self, args.concat(args2));
              }
            bound.prototype = Object.create(fun.prototype);
         return bound;
       };
scofieldfan commented 5 years ago
function Test(){
}
let bindTest = Test.bind({});
let a = new bindTest() //此时 this instanceof bound 是true。