防抖/节流中的apply起到的作用

防抖

function debounce(fn, delay) {
    let timerId = null;
    return function (...args) {
        if (timerId) {
            clearTimeout(timerId);
        }
        timerId = setTimeout(() => {
            fn.apply(this, args);
            timerId = null;
        }, delay);
    }
}

节流

function throttle(fn, delay) {
    let canUse = true;
    return function (...args) {
        //如果为true就触发方法
        if (canUse) {
            canUse = false;
            fn.apply(this, args);
            setTimeout(() => (canUse = true), delay);
        }
    };
}

apply

可以看到这两个函数都使用了apply,当然call或bind(bind返回的是一个函数需要()执行)达到的效果一样。
主要想分析一下是如何起效的,因为以前刚接触这两个函数的时候没有多想,而且使用许久在有时有意的没有用apply的时候也没有出现什么不同的情况。去网上各种搜基本都是简单的一行注释 // 将this正确指向(但说实话看到apply/call/bind都能写出这行注释)。
以下是我自己的分析,如果有什么不对希望指正。

  1. 如果回调函数是一个箭头函数,apply的this是不会起作用的。
  2. 希望起作用的场景一般是回调函数内部使用了this.xxx,且是一个function()写法,那么apply绑定的this会起效。要注意的是,在function()中,严格模式下没有调用者时,this的值为undefined,因此可以说此时如果希望使用this必须要有apply。
  3. 节流与防抖的返回值:返回值必须是function命名的函数而不是箭头函数,否则这个函数的this就等于debounce的this等于undefined。

首先理清楚顺序,debounce(handle,delay),那么debounce先执行,handle作为参数(因为handle没有带括号,其作为函数传入)。debounce内部又返回一个function,因此真正的回调函数是内部的返回的那个function。那么每次触发执行回调时这个function(也就是return的function)的this都会指向回调者,因此this也就有了值,所以func.apply(this,args)这里的this其实就是原来调用者的this。

以下是在stackoverflow上搜到的一个回答,问问题的人和我有同样的疑问。回答指出如下情况在没有apply时会空指针。

class Foo {
  constructor() {
    this.a = "a";
    this.bar = debounce(this.bar, 500);
  }
  bar() {
    console.log(this.a);
  }
}
const foo = new Foo();
foo.bar();

最后我自己的理解是,很多时候并没有使用this的情况下,这个并没有起到什么作用,包括我昨天看的一篇文章,作者直接说这个apply没用,只是为了更好的接收参数来替代arguments,反正也是挺惊讶的。那就这样吧。
如果上面描述的有错误或不合理的地方可以评论告知一下,感谢。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Vue,有一些常用的节流插件可以方便地实现节流的功能。以下是两个常用的插件: 1. Lodash(节流) Lodash是一个JavaScript实用工具库,它提供了许多常用的函数和方法,包括节流函数。使用Lodash的`debounce`和`throttle`函数可以很方便地实现节流。 安装Lodash: ```bash npm install lodash ``` 使用示例: ```javascript import { debounce, throttle } from 'lodash'; // 示例 const debouncedFunc = debounce(() => { console.log('执行操作'); }, 500); // 节流示例 const throttledFunc = throttle(() => { console.log('执行操作'); }, 200); ``` 2. Vue-lodash(节流) Vue-lodash是一个专门为Vue开发的Lodash插件,它提供了Vue指令的方式来使用Lodash的方法,包括节流。 安装Vue-lodash: ```bash npm install vue-lodash ``` 在Vue项目使用Vue-lodash示例: ```javascript import Vue from 'vue'; import VueLodash from 'vue-lodash'; import { debounce, throttle } from 'lodash'; Vue.use(VueLodash, { lodash: { debounce, throttle } }); ``` 在Vue组件使用节流: ```html <template> <div> <button v-debounce:click="debouncedFunc">点击按钮()</button> <button v-throttle:click="throttledFunc">点击按钮(节流)</button> </div> </template> <script> export default { methods: { debouncedFunc() { console.log('执行操作'); }, throttledFunc() { console.log('执行操作'); }, }, }; </script> ``` 以上是两个常用的Vue插件,可以方便地在Vue项目使用节流功能。根据具体需求选择合适的插件来使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值