0

i have question in my head. if i run the following code.

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

i guess the event callback will be overlay each other and result console.log('test') once, but what happened to those extra event listeners? do they get garbage collected and removed from memory? will it lead to memory leak if mutiple event been assigned with same function?

many thanks

1 Answer 1

2

what happened to those extra event listeners?

See the 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?

They never exist in the first place.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.