node.js中的events.emitter.removeAllListeners方法使用说明_node.js
程序员文章站
2022-03-31 19:17:27
...
方法说明:
emitter.removeAllListeners([event])
//移除所有监听器
emitter.removeAllListeners()
//移除指定event的所有监听器
emitter.removeAllListeners('data')
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;
};
移除所有监听器,如果指定event,则将移除指定事件的所有监听器。
语法:
复制代码 代码如下:
emitter.removeAllListeners([event])
接收参数:
event 事件类型,支持多个
例子:
复制代码 代码如下:
//移除所有监听器
emitter.removeAllListeners()
//移除指定event的所有监听器
emitter.removeAllListeners('data')
源码:
复制代码 代码如下:
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;
};
上一篇: 如何基于最大堆实现最大优先队列
推荐阅读
-
Node.js中关于多进程模块Cluster的详细介绍以及如何使用
-
node.js中的fs.linkSync方法使用说明_node.js
-
Node.js使用Express.Router的方法
-
node.js监听文件变化的实现方法
-
C#Url操作类封装、仿Node.Js中的Url模块实例
-
Node.js中的process.nextTick使用实例
-
推荐分享Node.js中18个值得了解的Web框架和工具
-
nodejs中简单实现Javascript Promise机制的实例_node.js
-
node.js请求HTTPS报错:UNABLE_TO_VERIFY_LEAF_SIGNATURE\的解决方法
-
Node.JS段点续传:Nginx配置文件分段下载功能的实现方法_node.js