off(eventName, listener)
Removes a specific listener for an event. If no listener is provided, all listeners for that event are removed.
Parameters:
eventName(string) – Event name.listener(function, optional) – Specific listener to remove.
Returns:
The EventEmitter instance (supports chaining).
Example:
1const fn = (msg) => console.log(msg);2emitter.on("data", fn);3emitter.off("data", fn); // Removes fn
Edge Cases:
- Calling
offwith no listener removes all listeners for the event. - Safe to call
offon an event that has no listeners; does nothing.