js手动实现bind

Function.prototype.bind=function(obj){
    var arg=Array.prototype.slice.call(arguments,1);
    var context=this;
    var bound=function(){
      arg=arg.concat(Array.prototype.slice.call(arguments));
      return context.apply(obj,arg);
    }
    var F=function(){}
    F.prototype=context.prototype;
    bound.prototype=new F();
    return bound;
}

function read(name, time, book) {
    console.log(`${name} is reading ${book} at ${time}`)
}

var TomRead = read.bind(this, 'Tom', 'morning')
TomRead('<万历十五年>')
console.log(TomRead.bind)

猜你喜欢

转载自blog.csdn.net/weixin_40821790/article/details/81178839
今日推荐