What is the purpose of testing the following condition?
if ('addEventListener' in htmlElement) {
// do sth
}
I have seen that in some code examples of event emitter constructors. I first thought it was to test whether an event was bound to a HTML element but it turns out the condition also returns true
for HTML elements with no events attached to them.
Edit:
Here's a real life example to give a bit more context.
This code checks whether the addEventListener
property is available. The method addEventListener
is a part of EventTarget interface. All DOM elements should have it, I think.