Home >Web Front-end >JS Tutorial >Instructions for using the events.emitter.removeAllListeners method in node.js_node.js

Instructions for using the events.emitter.removeAllListeners method in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:27:422293browse

Method description:

Remove all listeners. If event is specified, all listeners for the specified event will be removed.

Grammar:

Copy code The code is as follows:

emitter.removeAllListeners([event])

Receive parameters:

event event type, supports multiple

Example:

Copy code The code is as follows:

//Remove all listeners

emitter.removeAllListeners()

//Remove all listeners for the specified event

emitter.removeAllListeners('data')

Source code:

Copy code The 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;
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn