提问人:Brandi 提问时间:5/4/2023 最后编辑:Brandi 更新时间:5/5/2023 访问量:59
在 Debounce 中返回箭头函数与函数声明有什么区别?[复制]
What is the difference between returning an arrow function vs function declaration in Debounce? [duplicate]
问:
这个问题在这里已经有答案了:
“箭头函数”和“函数”是否等同/可互换? (4 个答案)
ES6 中箭头函数中的“this”指的是什么? (10 个答案)
你能解释一下为什么去抖动会绑定吗? (2 个答案)
7个月前关闭。
在学习 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);
};
}
答: 暂无答案
下一个:重写私有方法时的奇怪行为
评论
function
func.apply(this, args)
cb(args)
cb(...args)
undefined
this