TGViewer
JavaScript JavaScript @javascript · 31.1K subscribers
Post #2585 4.04K
CHALLENGE

class EventBus {
constructor() {
this.events = new Map();
}

on(event, callback) {
if (!this.events.has(event)) {
this.events.set(event, []);
}
this.events.get(event).push(callback);
}

emit(event, data) {
if (this.events.has(event)) {
this.events.get(event).forEach(callback => callback(data));
}
}
}

const bus = new EventBus();
bus.on('user', name => console.log(`Hello ${name}`));
bus.on('user', name => console.log(`Welcome ${name}`));
bus.emit('user', 'Sarah');
bus.emit('admin', 'John');
  • ❤ 6
  • 🤔 3
  • 👍 1
  • 🤣 1
More from @javascript
  1. Oct 2, 2026Post #3532
  2. Oct 2, 2026CHALLENGE const arr = [1, 2, 3]; const log = []; const result = arr.map((x, i) => { if (i…
  3. Oct 1, 2026Post #3530
  4. Oct 1, 2026CHALLENGE class Counter { #count = 0; static #instances = 0; constructor() { Counter.#inst…
  5. Sep 30, 2026Post #3528
  6. Sep 30, 2026CHALLENGE const message = 'a😀bc'; const codePoints = [...message]; const combined = { raw…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →