例如
//防抖(非立即执行)
function debounce(fn, delay = 500) {
let timer = null;
return function () {
if (timer) {
clearTimeout(timer);
timer = null
}
timer = setTimeout(() => {
fn.apply(this, arguments);
timer = null;
}, delay)
}
}
这个fn绑定this执行的意义何在? 求解
因为如果不绑定 this,会出现典型的 this 丢失的情况。
从这个
debounce
的代码看,它的功能是通过传入一个 fn,得到这个 fn 的延迟执行版本,方便后续调用。所以,可以假设一下用例:
如果用 apply 绑定了 this,运行结果是:
for test: testThis
。如果未绑定,运行结果是:
for test: undefined
。这是因为 test.debounceFn() 的 this 指向是这个 test 对象。但是 debounceFn 方法中的 fn 方法,作为参数传入 setTimeout 后,是独立运行的,this 指向丢失。
这里是通过 test.debounceFn -> 箭头函数 -> fn.apply 一步步把 test 对象作为 this 值绑定给了 fn。