首页 > 移动平台 > 详细

6.手写实现bind,call,apply

时间:2020-03-21 15:50:07      阅读:62      评论:0      收藏:0      [点我收藏+]
//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)

 

6.手写实现bind,call,apply

原文:https://www.cnblogs.com/chenlw/p/12539367.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!