apply、call、bind手写

手写apply、bind、call

apply

首先分析apply调用的时候主要传入俩种参数,第一个是目标this,第二个是需要调用的参数。参数可以是多个,最后都会传入到函数中,并且返回值是函数的返回值。

  • 获取需要执行的函数(就是 this,因为 call 的调用都是 fn.call(),fn 是一个函数)

  • 对 thisArg 进行转换成对象类型(防止传入的是一个非对象类型),这个 thisArg 就是需要绑定的 this

  • 调用需要被执行的函数(通过给 thisArg 增加方法属性)

Function.prototype.myApply = function (thisArg, args) {
  // 拿到函数体
  const fn = this;
  args = args || [];
  //需要的函数指向
  const thisArgs =
    thisArg !== null && thisArg !== undefined ? Object(thisArg) : Window;

  // 模板this上挂载函数
  thisArgs.fn = fn;
  const res = thisArgs.fn(...args);
  delete thisArgs.fn;
  return res;
};

bind

分析bind传入特点:主要传入两种参数:一个是目标this,第二个是需要调用的参数。参数可以是多个,最后都会传入到函数中,并且返回值是改变到目标this的函数。

  • 获取需要执行的函数(就是 this,因为 bind 的调用都是 fn.bind(),fn 是一个函数)

  • 对 thisArg 进行转换成对象类型(防止传入的是一个非对象类型),这个 thisArg 就是需要绑定的 this

  • 返回一个函数

  • 返回的函数内部调用需要被执行的函数(通过给 thisArg 增加方法属性)

  • 调用时将参数传递进去,返回的函数可能能够接收到参数,所以需要将返回函数的参数也传入进去

Function.prototype.myBind = function (thisArg, ...arg) {
  // 函数体
  const fn = this;

  // 获取this指向
  const thisArgs =
    thisArg !== null && thisArg !== undefined ? Object(thisArg) : Window;

  //   先在this上挂载fn。
  thisArgs.fn = fn;
  // 返回函数
  return (...arg1) => {
    thisArgs.fn(...arg, ...arg1);
    // 执行完毕要删除在函数体的挂载
    delete thisArgs.fn;
  };
};

call

分析call传入特点:主要传入两种参数:一个是目标this,第二个是需要调用的参数。参数可是一个数组,最后数组里面的参数会依次传入到函数中,并且返回值是改变到目标this的函数。

  • 获取需要执行的函数(就是 this,因为 call 的调用都是 fn.call(),fn 是一个函数)

  • 对 thisArg 进行转换成对象类型(防止传入的是一个非对象类型),这个 thisArg 就是需要绑定的 this

  • 调用需要被执行的函数(通过给 thisArg 增加方法属性)

Function.prototype.myCall = function (thisArg, ...args) {
  // 拿到函数体
  const fn = this;

  //需要的函数指向
  const thisArgs =
    thisArg !== null && thisArg !== undefined ? Object(thisArg) : Window;

  // 模板this上挂载函数
  thisArgs.fn = fn;
  const res = thisArgs.fn(...args);
  delete thisArgs.fn;
  return res;
};
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值