gpt4 book ai didi

javascript - 为可能存在性能问题的事件分配多个相同的回调函数?

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

我脑子里有疑问。如果我运行以下代码。

function test() { console.log('test'); }
event.addEventListner('click', test);
event.addEventListner('click', test);
event.addEventListner('click', test);

我猜事件回调将相互叠加并产生 console.log('test') 一次,但是那些额外的事件监听器发生了什么?它们会被垃圾收集并从内存中删除吗?多个事件分配同一个函数会导致内存泄漏吗?

非常感谢

最佳答案

what happened to those extra event listeners?

请参阅MDN documentation :

If multiple identical EventListeners are registered on the same EventTarget with the same parameters, the duplicate instances are discarded. They do not cause the EventListener to be called twice, and they do not need to be removed manually with the removeEventListener() method.

<小时/>

do they get garbage collected and removed from memory?

它们从一开始就不存在。

关于javascript - 为可能存在性能问题的事件分配多个相同的回调函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55083341/

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