node.js中的events.emitter.removeAllListeners方法使用说明
方法说明:
移除所有监听器,如果指定event,则将移除指定事件的所有监听器。
语法:
emitter.removeAllListeners([event])
接收参数:
event 事件类型,支持多个
例子:
//移除所有监听器
emitter.removeAllListeners()
//移除指定event的所有监听器
emitter.removeAllListeners('data')
源码:
EventEmitter.prototype.removeAllListeners=function(type){
varkey,listeners;
if(!this._events)
returnthis;
//notlisteningforremoveListener,noneedtoemit
if(!this._events.removeListener){
if(arguments.length===0)
this._events={};
elseif(this._events[type])
deletethis._events[type];
returnthis;
}
//emitremoveListenerforalllistenersonallevents
if(arguments.length===0){
for(keyinthis._events){
if(key==='removeListener')continue;
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events={};
returnthis;
}
listeners=this._events[type];
if(util.isFunction(listeners)){
this.removeListener(type,listeners);
}else{
//LIFOorder
while(listeners.length)
this.removeListener(type,listeners[listeners.length-1]);
}
deletethis._events[type];
returnthis;
};