addEventListener discards duplicates
The addEventListener method in HTML DOM has a unique behavior that is often overlooked. When adding an event listener, it automatically discards any duplicates. This means that if the event listener list of eventTarget already contains a listener with the same type, callback, and capture mode, the new listener will not be appended.
The rule only adds the listener if it doesn't already exist on the target with identical type, callback, and capture settings. However, there may be instances where you need to be notified twice, despite the duplicate removal policy. In such cases, you can register two different function objects that ultimately lead to the same underlying function.
For instance, you could wrap these functions inside separate ones. Normally, bind() is used to bind to the current this context. In this scenario, we bind to nothing since the logMessage function doesn't utilize the this keyword. Each bind operation generates a new function object, thus making them distinct callbacks in the context of addEventListener's duplicate elimination.
To reclaim the handler later if needed, you must remember it so that you can pass it to removeEventListener. Raymond, who has been part of the Windows evolution for over three decades, began a web site called The Old New Thing in 2003. This site gained immense popularity and has since grown beyond his expectations, even causing him to experience the heebie-jeebies.
The online platform led to the publication of a book with the same title, published by Addison Wesley in 2007. Occasionally, Raymond makes appearances on the Windows Dev Docs Twitter account to share stories that provide no practical information.
Rewritten by urgent.news; may contain errors — check the original.
Source: devblogs.microsoft.com