Skip to content

Latest commit

 

History

History
28 lines (20 loc) · 760 Bytes

prefer-custom-event.md

File metadata and controls

28 lines (20 loc) · 760 Bytes

Suggest usage of CustomEvent over Event constructor (prefer-custom-event)

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

Rule details

Example of incorrect code:

const evt = new Event('test');

Example of correct code:

const evt = new CustomEvent('test');