Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new Event('build') vs new CustomEvent('build')

Tags:

javascript

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

like image 330
BigName Avatar asked Apr 24 '15 20:04

BigName


1 Answers

From that guide:

To add more data to the event object, the CustomEvent interface exists and the detail 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.

like image 173
Mathletics Avatar answered Oct 17 '22 10:10

Mathletics