Call从简易到难的实现,简单易懂

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
  <script>

    // 最简化的版本
    //1、将函数设为对象的属性 2、执行该函数  3、删除该函数
    Function.prototype.call2 =function(context){
      context.fn = this;
     
      context.fn();
      delete context.fn;
    }
    var foo={
      name:'paopao'
    }
    function test(){
      console.log(this.name);
    }

    // test.call2(foo);

    // 不定长的参数,解决
    Function.prototype.call3 = function(context){

      context.fn = this;
      var args = [];
      for(let i=1;i<arguments.length;i++){
        // 或者使用字符串进行拼接
        args.push('arguments['+i+']');
        //使用模板字符串
        args.push(`arguments[${i}]`);
      }
      //eval能使字符串中的表达式像javascript的运行环境中一样去执行
      eval(`context.fn(${args})`);
      delete context.fn;
    }

    var foo1={
      name:'hello china'
    }
    function test1(age,color){
      console.log(this.name);
      console.log(age);
      console.log(color);
    }

    // test1.call3(foo1,12,'yellow');

    
    //最终版:解决返回值的问题和this的参数为null,这个时候默认指向window
    var name = 'ab';
    function test2(){
      console.log(this.name);
    }
    // test2.call(null)

    Function.prototype.call4 = function(context){
      var context = context || window;
      context.fn = this;
      var args = [];
      for(let i=1;i<arguments.length;i++){
        args.push(`arguments[${i}]`);
      }
      var res = eval(`context.fn(${args})`);
      
      delete context.fn;
      return res;
    }
    var name='lan paopao';
    var foo4 ={
      name:'paopao wudi'
    }
    function test4(age,color){
      console.log(this.name);
      return {
        name:this.name,
        age:age,
        color:color
      }
    }
    test4.call4(null);
    console.log(test4.call4(foo4,1233,'hahha'));

    

  </script>
</body>
</html>

ps:参照冴羽大佬

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
call、apply和bind都是用来改变函数中的this指向的方法。其中,call和apply可以直接调用函数并传递参数,而bind则返回一个新的函数,需要手动调用。 具体实现方案如下: - call的实现: 1. 给想要绑定的对象设置一个属性,并将该属性指向需要调用的函数。 2. 使用该对象调用函数,并传递参数。 3. 结束调用后,删除该属性。 - apply的实现: 1. 给想要绑定的对象设置一个属性,并将该属性指向需要调用的函数。 2. 使用该对象调用函数,并传递参数数组。 3. 结束调用后,删除该属性。 - bind的实现: 1. 创建一个新的函数,并将原函数作为其中的属性保存起来。 2. 当新函数被调用时,将之前绑定的对象作为this,并传递参数。 3. 返回新函数供后续调用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [如何实现call、apply、bind](https://blog.csdn.net/XIAO_A_fighting/article/details/116701887)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [原生JS实现 call apply bind](https://download.csdn.net/download/weixin_38628990/14046564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值