Is there any EventEmitter in browser side that has similar logic in NodeJS?

In modern browsers, there is EventTarget.

class MyClass extends EventTarget {
  doSomething() {
    this.dispatchEvent(new Event('something'));
  }
}

const instance = new MyClass();
instance.addEventListener('something', (e) => {
  console.log('Instance fired "something".', e);
});
instance.doSomething();

Additional Resources:

  • Maga Zandaqo has an excellent detailed guide here: https://medium.com/@zandaqo/eventtarget-the-future-of-javascript-event-systems-205ae32f5e6b

  • MDN has some documentation: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget

  • Polyfill for Safari and other incapable browsers: https://github.com/ungap/event-target

Leave a Comment

techhipbettruvabetnorabahisbahis forumueduseduedueduseduedueduedusedusedu