node.js中的events.emitter.removeListener方法使用说明_node.js
程序员文章站
2022-05-15 09:29:10
...
方法说明:
emitter.removeListener(event, listener)
var callback = function(stream) {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);
// 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 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;
};
移除指定事件的某个监听器。
语法:
复制代码 代码如下:
emitter.removeListener(event, listener)
接收参数:
event (string) 事件类型
listener (function) 已注册过的监听器
例子:
复制代码 代码如下:
var callback = function(stream) {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);
源码:
复制代码 代码如下:
// 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 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;
};
上一篇: php面向对象对象概念理解
下一篇: 用PHP书写安全的脚本代码_PHP教程
推荐阅读
-
详解Node.js开发中的express-session
-
node.js的mysql模块query方法里的字符串拼接问题
-
node.js入门教程之querystring模块的使用方法
-
Node.js利用js-xlsx处理Excel文件的方法详解
-
利用C/C++编写node.js原生模块的方法教程
-
Linux CentOS系统下安装node.js与express的方法
-
如何在Linux系统中利用node.js提取Word及PDF文本内容的案例介绍
-
Node.js中的异步生成器与异步迭代详解
-
Node.js对MongoDB数据库实现模糊查询的方法
-
node.js中的fs.unlink方法使用说明_node.js