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

问题描述 投票:0回答:0

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

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

VS

function debounce(func, delay){
   let timeoutId;
   return (...args) => {
       clearTimeout(timeoutId);
       timeoutId = setTimeout(() => {
          func.apply(this, args);
       }, delay);
   };
}
javascript this arrow-functions
© www.soinside.com 2019 - 2024. All rights reserved.