6.手写实现bind,call,apply

//bind
Function.prototype.bind1 = function(){
  let args = Array.prototype.slice.call(arguments);
  let t = args.shift();
  let self = this;
  return function(){
        return self.apply(t,agrs);
    }        
}
//call
 Function.prototype.call1 = function(t,...args){
       let self = this;
       return (function(){
           return self.apply(t,args);
       })()
    }
//apply
 Function.prototype.apply1 = function (t,args) {
        let self = this;
        return (function(){
            console.log(...args)
            return self.call(t,...args); 
        })()
    }
//调用

  function fn1(a, b) {
            console.log(this)
            console.log(a, b)
            return `this is fn1`
    
    }
    // const fn2 = fn1.bind({x:100},10,20)
    // fn2();
    // const fn2 = fn1.apply1({x:100},[19,20])
    
    const fn3 = fn1.call1({x:100},19,30)
原文地址:https://www.cnblogs.com/chenlw/p/12539367.html