如果我调用函数once
这样
var button = document.querySelector('button');
button.addEventListener('click', once);
function once() {
console.log('one');
button.removeEventListener('click', once);
}
它只打电话一次。
但是如果我这样打 once()
var button = document.querySelector('button');
button.addEventListener('click', once());
function once() {
console.log('one');
button.removeEventListener('click', once());
}
异常抛出
异常:InternalError:过多的递归
您能否解释为什么会这样。
()
在函数名称调用函数之后。因此,当button.addEventListener('click', once());
您绑定时,once()
方法的返回值是不确定的。
并且由于once()
没有任何break语句就以递归方式进行调用,因此您会收到InternalError:过多的递归。
您应该传递函数引用。
button.addEventListener('click', once);
有关其他信息:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句