Web components
DON'T USE CUSTOMEVENT IN JAVASCRIPT. INHERIT FROM EVENT INSTEAD.
It's commonplace in Web Components to use custom events. The custom event type is an inheritor of
the Event class, but it includes a new field, detail: any
, that allows you to attach data to the
Event, which is useful for passing data up to the listener, right?
Wrong. It's a trap. You could, theoretically, inherit from CustomEvent and narrow the content of
detail
to what you specifically want, and I've seen lots of code where people do exactly that.
But there's a better way: just inherit from Event. Skip the CustomEvent class and just create your own events. I'll show you how.
Continue Reading