On this MDN page Creating and Triggering Events it shows an example of creating event with Event
or CustomEvent
. It explains that CustomEvent
allows for custom details, but other than that, it doesn't say much.
So, what is the difference? If I'm creating a generic scroll event, should I use CustomEvent
? Or is it only for events not exist in javascript?
Also, I noticed that MouseEvent
is also a child of Event
, so if I'm creating a click event, I can just use new MouseEvent('click')
?
Thanks
From that guide:
To add more data to the event object, the
CustomEvent
interface exists and thedetail
property can be used to pass custom data.
You can use Event()
for anything. If you want to attach custom data, you use CustomEvent('eventName', {data})
.
And yes, for mouse-events, you should use MouseEvent
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With