手写代码call,apply,bind


一、call和apply是什么?

call 和 apply 都是为了解决改变 this 的指向。作用都是相同的,只是传参的方式不同。
除了第一个参数外,call 可以接收一个参数列表,apply 只接受一个参数数组

二、手写call代码以及注释~

 //1, call简单应用
            // var foo={
            //       value:'1'
            // }
            // function bar(){
            //       console.log(this.value);
            // }
            // bar.call(foo)

            //2,将bar函数挂载到foo对象上,使其成为foo的方法
            // var foo={
            //       value:1,
            //       
            //       bar:function(){
            //             console.log(this.value);
            //       }
            // };
            // foo.bar()
            //3,模拟call方法可以使函数成为对象的属性,通过调用对象的属性来用该函数
         Function.prototype.mycall=function(context){
               const fn =Symbol('fn');
               context=context||window//context是{e:3}
               context.fn=this
            //    console.log(this);//这里的this是g函数,把g函数绑定到context{e:3}上
               const args=[...arguments].slice('1')//把后面的参数分离出来
               const result=context.fn(...args)
               delete context.fn//删除context.fn属性
               return result

         }
         function g(a,b){
               console.log(a,b);
               console.log(this.e);

         }
         g.mycall({e:3},1,2)

三、手写apply代码以及注释

   Function.prototype.mycall=function(context){
               const fn =Symbol('fn');
               context=context||window//context是{e:3}
               context.fn=this
            //    console.log(this);//这里的this是g函数,把g函数绑定到context{e:3}上
               const args=[...arguments].slice(1)//把后面的参数分离出来
            //    console.log(args);
               const result=context.fn(args)//apply和call的区别在这,apply传过来的是一个数组
               delete context.fn//删除context.fn属性
               return result

         }
         function g(a){
               console.log(a);//这里输出的也是一个数组
               console.log(this.e);

         }
         g.mycall({e:3},[1,2])

三、手写bind代码以及注释

bind会返回一个函数

  //1,简单应用
      //  var bar={
      //        a:1
      //  }
      //  function b(){
      //        console.log(this.a);
      //  }
      //  b.bind(bar)()	
 //2,手写
      //先手写一个call
      Function.prototype.mycall=function(context){
           const fn=Symbol('fn')
           context=context||window
           context.fn=this
           const args=[...arguments].slice(1)
           const result=context.fn(...args)
           delete context.fn
           return result
      }
      Function.prototype.mybind=function (context){
            //这里返回一个函数,箭头函数保证this的指向是Function
            return ()=>{
                   return this.mycall(context)
            }
      }
      var obj={a:1};
      function bar(){
            console.log(this.a);
      }
      bar.mybind(obj)()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值