← All Exercises

Event Emitter Class

advancedjavascript~30min

Background

Event emitters are foundational to Node.js architecture, browser events, and reactive programming patterns.

Problem

Create an EventEmitter class with on(event, callback), off(event, callback), emit(event, data), and once(event, callback) methods.

Input

No input; the emitter is initialized empty with no registered listeners.

Output

An EventEmitter instance with event registration and emission capabilities.

Sample tests

Sample 1

Input: on("test", fn), emit("test", data)
Expected: fn called with data

Your solution

Learning objective

Master the observer pattern, callback management, and event-driven architecture.