Skip to content

Instantly share code, notes, and snippets.

@ccnokes
Created October 30, 2019 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccnokes/43ac4567113f9df318dfac01252b5ecd to your computer and use it in GitHub Desktop.
Save ccnokes/43ac4567113f9df318dfac01252b5ecd to your computer and use it in GitHub Desktop.
Event emitter using the native DOM APIs: EventTarget and CustomEvent
// Who needs eventemitter3, mitt, or some other library when you can use native DOM APIs? 😁
let eventEmitter = new EventTarget();
eventEmitter.addEventListener('test', console.log); // CustomEvent { type: 'test', detail: 123, ... }
eventEmitter.dispatchEvent(new CustomEvent('test', { detail: 123 }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment