在 Debounce 中返回箭头函数与函数声明有什么区别?[复制]

What is the difference between returning an arrow function vs function declaration in Debounce? [duplicate]

提问人:Brandi 提问时间:5/4/2023 最后编辑:Brandi 更新时间:5/5/2023 访问量:59

问:

在学习 JS 中的 Debounce 时,我看到了 2 种不同的方法。在函数声明示例中调用回调与在箭头函数示例中使用 apply 绑定进行调用之间有区别吗?

function debounce(cb, delay) {
   let timeoutId;
   return function(...args) {
       clearTimeout(timeoutId)
       timeoutId = setTimeout(() => {
           cb(args)
       }, delay);
   }
}

function debounce(func, delay){
   let timeoutId;
   return (...args) => {
       clearTimeout(timeoutId);
       timeoutId = setTimeout(() => {
          func.apply(this, args);
       }, delay);
   };
}
javascript 这个 箭头函数

评论

0赞 epascarello 5/4/2023
这些功能不同。第二个没有抖动......
0赞 Brandi 5/4/2023
哎呀,对不起,我刚刚编辑了它!
0赞 Guillaume Brunerie 5/4/2023
您是否对箭头函数和使用关键字声明的函数之间的区别有疑问?例如,参见 stackoverflow.com/questions/34361379/...function
0赞 Brandi 5/5/2023
我正在尝试确认它们在所有用例中是否相同,或者是否存在缺陷(可能返回函数声明)。如果这两个例子都是一样的,我想我想真正看看它,弄清楚它们是如何相同的。
1赞 Bergi 5/5/2023
它们不等同。 显然不同于(顺便说一句)存在非值 .func.apply(this, args)cb(args)cb(...args)undefinedthis

答: 暂无答案