gpt4 book ai didi

javascript - 传入off函数的匿名函数是什么意思?

转载 作者:行者123 更新时间:2023-12-01 00:51:29 24 4
gpt4 key购买 nike

传入off函数的匿名函数是什么意思?

例如,以下内容是什么意思:

$jqueryEl.off("click", function() { console.log("什么时候调用?"); });.

一旦所有点击监听器被删除,function() { console.log("When will it be call?"); 是否会被调用,或者是否会调用它而不是被删除的点击监听器?

我问这个是因为 the documentation 中没有明确的答案:

A handler function previously attached for the event(s), or the special value false.

最佳答案

What does the anonymous function passed into the off function mean?

没什么用处。

由于它是一个函数表达式,因此它会生成一个函数。因此它不能之前已附加到该事件

它也不是

编写该代码的人犯了一个错误。

<小时/>

通常,当有人打算有条件地删除该函数时,就会发生这种情况:

const myHandler = function() { console.log("When will it be called?"); };

$jqueryEl.on("click", myHandler);

$somethingElse.on("click", function () {
$jqueryEl.off("click", myHandler);
});

…但是当他们创建了两个相同的函数时,而不是为两个不同的调用重用相同的函数。

关于javascript - 传入off函数的匿名函数是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56935179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com