实现call、apply、bind

 

一、实现myCall

首先,call函数是将this绑定到传入的第一个参数上,其余参数作为调用函数的参数。

func.call(thisArg, arg1, arg2, ...);

那么,分为三步:

  1. 将 this 赋值给 thisArg ,作为它的某个函数属性
  2. 调用这个函数属性
  3. 删除这个属性

方法一:采用数组扩展符

Function.prototype.myCall = function (thisArg, ...arguments) {
  const func = Symbol();
  thisArg[func] = this;
  const res = thisArg[func](...arguments);
  delete thisArg[func];
  return res;
}

方法二:采用eval执行

Function.prototype.myCall = function (context) {
  var thisArg = context[0];
  thisArg.fn = this;
  var evalStr = 'thisArg.fn(';
  for (var i = 1; i < context.length; i++) {
    if (typeof context[i] === 'string') {
      evalStr += `"${context[i]}",`; // 对字符串情况的处理
    } else {
      evalStr += `${context[i]},`;
    }
  }
  var res = eval(evalStr);
  delete thisArg.fn;
  return res;
}

 

二、实现myApply

同myCall,只是参数需以数组的形式。

这里只写一种方法,另一种类似 call。

Function.prototype.myApply = function(thisArg, argArray) {
  const fn = Symbol();
  thisArg[fn] = this;
  const res = thisArg[fn](...argArray);
  delete thisArg[fn];
  return res;
}

三、实现myBind

bind函数返回一个新的函数,例如:

const fun2 = fun1.bind(obj, arg1, arg2);
fun2(arg3, arg4);
// 相当于
fun1.call(obj, arg1, arg2, arg3, arg4);

那么,可以写出:

Function.prototype.myBind = function(thisArg, ...argArray) {
  const self = this; // 这里先将此刻的this暂存起来
  return function(...argArray2) {
    const func = Symbol();
    thisArg[func] = self;
    const res = thisArg[func](...argArray, ...argArray2);
    delete thisArg[func];
    return res;
  }
}

这里只写一种方法,另一种类似 call。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
call、applybind都是用来改变函数中的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、applybind](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
发出的红包

打赏作者

进击的桐人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值