CustomEvent
is a type of DOM Event
that can also carry any data. The main benefit of creating an event using the CustomEvent
constructor over Event
is that the detail
property carrying the data is readonly.
const evt1 = new CustomEvent('test', { detail: 'original' });
evt1.detail = 'updated'; // (throws an error in strict mode)
console.log(evt1.detail); // original
const evt2 = new Event('test');
evt2.detail = 'original';
evt2.detail = 'updated';
console.log(evt2.detail); // updated
Example of incorrect code:
const evt = new Event('test');
Example of correct code:
const evt = new CustomEvent('test');