nodejs EventEmitter
2015-01-28 15:31
267 查看
Many objects in Node emit events: a
Typically, event names are represented by a camel-cased string, however,there aren't any strict restrictions on that, as any string will be accepted.
Functions can then be attached to objects, to be executed when an eventis emitted. These functions are called
listeners. Inside a listenerfunction,
When an
All EventEmitters emit the event
Returns emitter, so calls can be chained.
Returns
例子:
结果输出:
net.Serveremits an event each timea peer connects to it, a
fs.readStreamemits an event when the file isopened. All objects which emit events are instances of
events.EventEmitter.You can access this module by doing:
require("events");
Typically, event names are represented by a camel-cased string, however,there aren't any strict restrictions on that, as any string will be accepted.
Functions can then be attached to objects, to be executed when an eventis emitted. These functions are called
listeners. Inside a listenerfunction,
thisrefers to the
EventEmitterthat the listener wasattached to.
Class: events.EventEmitter#
To access the EventEmitter class,require('events').EventEmitter.
When an
EventEmitterinstance experiences an error, the typical action isto emit an
'error'event. Error events are treated as a special case in node.If there is no listener for it, then the default action is to print a stacktrace and exit the program.
All EventEmitters emit the event
'newListener'when new listeners areadded and
'removeListener'when a listener is removed.
emitter.addListener(event, listener)#
emitter.on(event, listener)#
Adds a listener to the end of the listeners array for the specifiedevent.No checks are made to see if the
listenerhas already been added. Multiplecalls passing the same combination of
eventand
listenerwill result in the
listenerbeing added multiple times.
server.on('connection', function (stream) { console.log('someone connected!'); });
Returns emitter, so calls can be chained.
emitter.emit(event, [arg1], [arg2], [...])#
Execute each of the listeners in order with the supplied arguments.Returns
trueif event had listeners,
falseotherwise.
例子:
var EventEmitter = require('events').EventEmitter; var event = new EventEmitter(); event.on('some_event', function(who, opcode, opobject){ console.log("listener 1 response to some_event"); console.log("%s %s %s\n", who, opcode, opobject); }); event.on('some_event', function(){ console.log("listener 2 response to some_event"); }); setTimeout(function(){ event.emit('some_event', "xiaoming", "eat", "rice"); }, 1000);
结果输出:
listener 1 response to some_event xiaoming eat rice listener 2 response to some_event
相关文章推荐
- 从零开始学习Node.js系列教程六:EventEmitter发送和接收事件的方法示例
- node.js中的events.EventEmitter.listenerCount方法使用说明
- Node.js 学习(六)Node.js EventEmitter
- Node.js EventEmitter
- Node.js EventEmitter事件发射器
- Node.js EventEmitter
- Node.js EventEmitter
- NodeJS学习系列课程笔记(NodeJs EventEmitter相关使用方法)
- Node.js EventEmitter
- node.js中的events.EventEmitter.listenerCount方法使用说明
- Node.js EventEmitter
- Node.js EventEmitter
- node.js EventEmitter发送和接收事件
- Node.js EventEmitter
- Node.js EventEmitter
- Node.js之EventEmitter开发
- Node.js EventEmitter
- Node.js EventEmitter
- 关于Node.js的events.EventEmitter
- node.js之EventEmitter