node.js中的events.emitter.removeListener方法使用說明
更新時(shí)間:2014年12月10日 09:46:31 投稿:junjie
這篇文章主要介紹了node.js中的events.emitter.removeListener方法使用說明,本文介紹了events.emitter.removeListener的方法說明、語法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下
方法說明:
移除指定事件的某個(gè)監(jiān)聽器。
語法:
復(fù)制代碼 代碼如下:
emitter.removeListener(event, listener)
接收參數(shù):
event (string) 事件類型
listener (function) 已注冊(cè)過的監(jiān)聽器
例子:
復(fù)制代碼 代碼如下:
var callback = function(stream) {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);
源碼:
復(fù)制代碼 代碼如下:
// emits a 'removeListener' event iff the listener was removed
EventEmitter.prototype.removeListener = function(type, listener) {
var list, position, length, i;
if (!util.isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events || !this._events[type])
return this;
list = this._events[type];
length = list.length;
position = -1;
if (list === listener ||
(util.isFunction(list.listener) && list.listener === listener)) {
delete this._events[type];
if (this._events.removeListener)
this.emit('removeListener', type, listener);
} else if (util.isObject(list)) {
for (i = length; i-- > 0;) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener)) {
position = i;
break;
}
}
if (position < 0)
return this;
if (list.length === 1) {
list.length = 0;
delete this._events[type];
} else {
list.splice(position, 1);
}
if (this._events.removeListener)
this.emit('removeListener', type, listener);
}
return this;
};
您可能感興趣的文章:
- node.js中的events.emitter.once方法使用說明
- 詳解Node.js:events事件模塊
- node.js中的events.emitter.removeAllListeners方法使用說明
- node.js中的events.EventEmitter.listenerCount方法使用說明
- 關(guān)于Node.js的events.EventEmitter用法介紹
- node.js中的events.emitter.listeners方法使用說明
- node.js學(xué)習(xí)之事件模塊Events的使用示例
- 詳解如何模擬實(shí)現(xiàn)node中的Events模塊(通俗易懂版)
- nodejs事件的監(jiān)聽與觸發(fā)的理解分析
- Node.js中的事件驅(qū)動(dòng)編程詳解
- Nodejs中自定義事件實(shí)例
- node.js中事件觸發(fā)器events的使用方法實(shí)例分析
相關(guān)文章
使用pkg打包nodejs項(xiàng)目并解決本地文件讀取的問題
這篇文章主要介紹了使用pkg打包nodejs項(xiàng)目并解決本地文件讀取的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10使用?Node.js和Express搭建服務(wù)器的過程步驟詳解
Node.js?是一個(gè)開源、跨平臺(tái)的?JavaScript?運(yùn)行時(shí)環(huán)境,這篇文章主要介紹了如何使用?Node.js和Express搭建服務(wù)器,需要的朋友可以參考下2023-09-09使用Node.js腳本自動(dòng)統(tǒng)計(jì)代碼量的實(shí)現(xiàn)代碼
手動(dòng)統(tǒng)計(jì)代碼行數(shù)通常會(huì)耗費(fèi)大量時(shí)間和精力,為了提高統(tǒng)計(jì)效率并減少人為錯(cuò)誤,我們可以借助自動(dòng)化工具來完成這項(xiàng)任務(wù),本文將介紹如何使用 Node.js 腳本來自動(dòng)化統(tǒng)計(jì)項(xiàng)目代碼行數(shù),讓我們能夠輕松快捷地獲取項(xiàng)目的代碼量信息,需要的朋友可以參考下2023-12-12