0条评论
还没有人评论过~
apply和call;拿函数防抖来解释:
function debounce(func, wait, immediate) {
let timer;
return function() {
let context = this,
args = arguments;
if (timer) clearTimeout(timer);
if (immediate) {
let callNow = !timer;
timer = setTimeout(() => {
timer = null;
}, wait);
if (callNow) func.apply(context, args);
} else {
timer = setTimeout(() => {
func.apply
}, wait)
}
}
}
这里面两个apply到底做了那些事?为什么要这么做?我到现在只觉得是为了传参,哪位大佬能给解个惑?拜谢
https://github.com/mqyqingfeng/Blog/issues/22#issuecomment-357874221
根据 func
参数值动态调用对应的 function