Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> This approach isn't recommended because we can only attach one handler for each event.

Except when you do want that behavior. E.g., you only want to set one listener and have a stream of data coming in that can trigger various attachments/detachments to the same element(s) over and over.

With the ancient "on" methods you get a single method that you can replace at will. Unbinding happens automatically. For example, you can keep assigning to an anonymous function till your heart's content and there will only ever be one callback in that slot attached as the author states.

With the new, "recommended" methods you must manually track the state and unbind old callbacks. For example, you would have leaky behavior if you kept assigning anonymous functions with addEventListener. And you can't easily hack your way out of the state problem because the DOM doesn't give you a way to fetch the current number of listeners, reset them to default or really know anything about them.

So the recommended behavior does not include in its design the ability to achieve the useful old behavior.

But at least DOM does have a convenience method to toggle class that will save a good 10 seconds per year of dev time.



What's the context for that quote? The main page of the linked site doesn't have it.

I was wondering something similar while looking at https://htmldom.dev/toggle-password-visibility

This could be done inline with <input type="password" id="password" /> <button onclick="password.type = password.type == 'text' ? 'password' : 'text'">Toggle</button>

...instead of 10 lines of code. Is it ever ok to use the fact that window.elementID works? even for exposition? (it works in all browsers, not sure if its standardized...) Why use setAttribute? Again, setting via element.type = ... works in all browsers.


It does. https://htmldom.dev/attach-or-detach-an-event-handler

The idea behind addEventListener is that multiple handlers can be attached to the same event of the same node, whereas before handlers were property assignments that would clobber each other.

The disadvantage of addEventListener is that event handlers are associated with event listeners instead of with DOM nodes, which makes them more challenging to garbage collect and thus increases the memory space of your application. The way to keep the code clean is to use removeEventListener when the handler is no longer needed. Event handlers are rarely removed from the code though, because of a lack of discipline and because many times its hard to tell when they are no longer needed.

The biggest advantage of assignment by property approach is that it imposes simplicity. Simplify means to make fewer and complicate means to make many. When there is only one handler it is inherently the most simple approach, which greatly increases code cleanliness and forces a greater concern of code organization.


Another possible advantage for onevent properties is they work as soon as the element is loaded. Attaching event listeners normally has to wait for the whole DOM to be loaded. The difference probably isn't noticeable over a good connection.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: