Method description:
Removes all listeners, and if an event is specified, all listeners for the specified event are removed.
grammar:
The code copy is as follows:
emitter.removeAllListeners([event])
Receive parameters:
event event type, supports multiple
example:
The code copy is as follows:
//Remove all listeners
emitter.removeAllListeners()
//Remove all listeners of the specified event
emitter.removeAllListeners('data')
Source code:
The code copy is as follows:
EventEmitter.prototype.removeAllListeners = function(type) {
var key, listeners;
if (!this._events)
return this;
// not listening for removeListener, no need to emit
if (!this._events.removeListener) {
if (arguments.length === 0)
this._events = {};
else if (this._events[type])
delete this._events[type];
return this;
}
// emit removeListener for all listeners on all events
if (arguments.length === 0) {
for (key in this._events) {
if (key === 'removeListener') continue;
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = {};
return this;
}
listeners = this._events[type];
if (util.isFunction(listeners)) {
this.removeListener(type, listeners);
} else {
// LIFO order
While (listeners.length)
this.removeListener(type, listeners[listeners.length - 1]);
}
delete this._events[type];
return this;
};