js关于更改this指向问题

2020-10-09 17:05发布

问题:

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到底做了那些事?为什么要这么做?我到现在只觉得是为了传参,哪位大佬能给解个惑?拜谢

回答1:

https://github.com/mqyqingfeng/Blog/issues/22#issuecomment-357874221



回答2:

根据 func 参数值动态调用对应的 function



标签: js