Events. emitter. removeAllListeners method in node. js
Method description:
Removes all listeners. If an event is specified, all listeners of the specified event are removed.
Syntax:
Copy codeThe Code is as follows:
Emitter. removeAllListeners ([event])
Receiving parameters:
Event type. Multiple event types are supported.
Example:
Copy codeThe Code is as follows:
// Remove all listeners
Emitter. removeAllListeners ()
// Remove all listeners of the specified event
Emitter. removeAllListeners ('data ')
Source code:
Copy codeThe Code 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;
};