node.js中的emitter.emit方法使用说明_node.js
程序员文章站
2022-05-16 13:38:27
...
方法说明:
emitter.emit(event, [arg1], [arg2], [...])
var events = require('events');
var emitter = new events.EventEmitter();
emitter.on('someEvent', function(arg1, arg2){
console.log('listener1', arg1, arg2);
})
emitter.on('someEvent', function(arg1, arg2){
console.log('listener2', arg1, arg2);
})
emitter.emit('someEvent', 'byvoid', 1991);
EventEmitter.prototype.emit = function(type) {
var er, handler, len, args, i, listeners;
if (!this._events)
this._events = {};
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events.error ||
(util.isObject(this._events.error) && !this._events.error.length)) {
er = arguments[1];
if (this.domain) {
if (!er) er = new TypeError('Uncaught, unspecified "error" event.');
er.domainEmitter = this;
er.domain = this.domain;
er.domainThrown = false;
this.domain.emit('error', er);
} else if (er instanceof Error) {
throw er; // Unhandled 'error' event
} else {
throw TypeError('Uncaught, unspecified "error" event.');
}
return false;
}
}
handler = this._events[type];
if (util.isUndefined(handler))
return false;
if (this.domain && this !== process)
this.domain.enter();
if (util.isFunction(handler)) {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
len = arguments.length;
args = new Array(len - 1);
for (i = 1; i args[i - 1] = arguments[i];
handler.apply(this, args);
}
} else if (util.isObject(handler)) {
len = arguments.length;
args = new Array(len - 1);
for (i = 1; i args[i - 1] = arguments[i];
listeners = handler.slice();
len = listeners.length;
for (i = 0; i listeners[i].apply(this, args);
}
if (this.domain && this !== process)
this.domain.exit();
return true;
};
发射event事件,传递若干可选参数到事件监听器的参数表。
语法:
复制代码 代码如下:
emitter.emit(event, [arg1], [arg2], [...])
接收参数:
event 事件类型
arg1 ~ argN 传递的参数(多个)
例子:
复制代码 代码如下:
var events = require('events');
var emitter = new events.EventEmitter();
emitter.on('someEvent', function(arg1, arg2){
console.log('listener1', arg1, arg2);
})
emitter.on('someEvent', function(arg1, arg2){
console.log('listener2', arg1, arg2);
})
emitter.emit('someEvent', 'byvoid', 1991);
源码:
复制代码 代码如下:
EventEmitter.prototype.emit = function(type) {
var er, handler, len, args, i, listeners;
if (!this._events)
this._events = {};
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events.error ||
(util.isObject(this._events.error) && !this._events.error.length)) {
er = arguments[1];
if (this.domain) {
if (!er) er = new TypeError('Uncaught, unspecified "error" event.');
er.domainEmitter = this;
er.domain = this.domain;
er.domainThrown = false;
this.domain.emit('error', er);
} else if (er instanceof Error) {
throw er; // Unhandled 'error' event
} else {
throw TypeError('Uncaught, unspecified "error" event.');
}
return false;
}
}
handler = this._events[type];
if (util.isUndefined(handler))
return false;
if (this.domain && this !== process)
this.domain.enter();
if (util.isFunction(handler)) {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
len = arguments.length;
args = new Array(len - 1);
for (i = 1; i args[i - 1] = arguments[i];
handler.apply(this, args);
}
} else if (util.isObject(handler)) {
len = arguments.length;
args = new Array(len - 1);
for (i = 1; i args[i - 1] = arguments[i];
listeners = handler.slice();
len = listeners.length;
for (i = 0; i listeners[i].apply(this, args);
}
if (this.domain && this !== process)
this.domain.exit();
return true;
};
推荐阅读
-
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
-
在Node.js中使用jQuery的方法
-
一个简单的node.js界面实现方法
-
node.js中实现kindEditor图片上传功能的方法教程