I’ve written my own JavaScript class (for a custom web component). It raises a couple of events. For instance: TagChanged
To listen to this event, I (obviously) do this:
myObject.addEventListener('TagChanged'), (e) => doSomething());
But is it possible to have something like:
myObject.onTagChanged = (e) => doSomething();
The default DOM elements have things like onClick
and onKeyDown
. Are they using some other architecture?
2
Answers
You’d could implement something yourself, like this:
Yes, Browser code us mortals can not access
But that Browser code also adds a
handleEvent
on every JavaScript ObjectFrom MDN – add EventListener
So you can do
which (by default) calls
handleEvent
Then triggers your method:
Full code