知识共享许可协议
本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可。

Node.js v0.10.18 手册 & 文档


事件 (Events)#

稳定度: 4 - 冻结

Many objects in Node emit events: a net.Server emits an event each time a peer connects to it, a fs.readStream emits an event when the file is opened. All objects which emit events are instances of events.EventEmitter. You can access this module by doing: require("events");

Node里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时分发一个事件, 一个fs.readStream对象会在文件被打开的时候发出一个事件。 所有这些产生事件的对象都是 events.EventEmitter 的实例。 你可以通过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.

通常,事件名是驼峰命名 (camel-cased) 的字符串。不过也没有强制的要求,任何字符串都是可以使用的。

Functions can then be attached to objects, to be executed when an event is emitted. These functions are called listeners. Inside a listener function, this refers to the EventEmitter that the listener was attached to.

为了处理发出的事件,我们将函数 (Function) 关联到对象上。 我们把这些函数称为 监听器 (listeners)。 在监听函数中 this 指向当前监听函数所关联的 EventEmitter 对象。

类: events.EventEmitter#

To access the EventEmitter class, require('events').EventEmitter.

通过 require('events').EventEmitter 获取 EventEmitter 类。

When an EventEmitter instance experiences an error, the typical action is to 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 stack trace and exit the program.

EventEmitter 实例遇到错误,通常的处理方法是产生一个 'error' 事件,node 对错误事件做特殊处理。 如果程序没有监听错误事件,程序会按照默认行为在打印出 栈追踪信息 (stack trace) 后退出。

All EventEmitters emit the event 'newListener' when new listeners are added and 'removeListener' when a listener is removed.

EventEmitter 会在添加 listener 时触发 'newListener' 事件,删除 listener 时触发 'removeListener' 事件

emitter.addListener(event, listener)#

emitter.on(event, listener)#

emitter.addListener(event, listener)#

emitter.on(event, listener)#

Adds a listener to the end of the listeners array for the specified event.

添加一个 listener 至特定事件的 listener 数组尾部。

server.on('connection', function (stream) {
  console.log('someone connected!');
});

Returns emitter, so calls can be chained.

返回 emitter,方便链式调用。

emitter.once(event, listener)#

Adds a one time listener for the event. This listener is invoked only the next time the event is fired, after which it is removed.

添加一个 一次性 listener,这个 listener 只会在下一次事件发生时被触发一次,触发完成后就被删除。

server.once('connection', function (stream) {
  console.log('Ah, we have our first user!');
});

Returns emitter, so calls can be chained.

返回 emitter,方便链式调用。

emitter.removeListener(event, listener)#

Remove a listener from the listener array for the specified event. Caution: changes array indices in the listener array behind the listener.

从一个事件的 listener 数组中删除一个 listener 注意:此操作会改变 listener 数组中当前 listener 后的所有 listener 在的下标

var callback = function(stream) {
  console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);

Returns emitter, so calls can be chained.

返回 emitter,方便链式调用。

emitter.removeAllListeners([event])#

Removes all listeners, or those of the specified event.

删除所有 listener,或者删除某些事件 (event) 的 listener

Returns emitter, so calls can be chained.

返回 emitter,方便链式调用。

emitter.setMaxListeners(n)#

By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default which helps finding memory leaks. Obviously not all Emitters should be limited to 10. This function allows that to be increased. Set to zero for unlimited.

在默认情况下,EventEmitter 会在多于 10 个 listener 监听某个事件的时候出现警告,此限制在寻找内存泄露时非常有用。 显然,也不是所有的 Emitter 事件都要被限制在 10 个 listener 以下,在这种情况下可以使用这个函数来改变这个限制。

Returns emitter, so calls can be chained.

返回 emitter,方便链式调用。

EventEmitter.defaultMaxListeners#

emitter.setMaxListeners(n) sets the maximum on a per-instance basis. This class property lets you set it for all EventEmitter instances, current and future, effective immediately. Use with care.

emitter.setMaxListeners(n) 设置每个 emitter 实例的最大监听数。 这个类属性为 所有 EventEmitter 实例设置最大监听数(对所有已创建的实例和今后创建的实例都将立即生效)。 使用时请注意。

Note that emitter.setMaxListeners(n) still has precedence over EventEmitter.defaultMaxListeners.

请注意,emitter.setMaxListeners(n) 优先于 EventEmitter.defaultMaxListeners

emitter.listeners(event)#

Returns an array of listeners for the specified event.

返回指定事件的 listener 数组

server.on('connection', function (stream) {
  console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection'))); // [ [Function] ]

emitter.emit(event, [arg1], [arg2], [...])#

Execute each of the listeners in order with the supplied arguments.

使用提供的参数按顺序执行指定事件的 listener

Returns true if event had listeners, false otherwise.

若事件有 listener 则返回 true 否则返回 false

类方法: EventEmitter.listenerCount(emitter, event)#

Return the number of listeners for a given event.

返回指定事件的 listener 数量

事件: 'newListener'#

  • event String The event name
  • listener Function The event handler function

  • event String 事件名

  • listener Function 事件处理函数

This event is emitted any time someone adds a new listener. It is unspecified if listener is in the list returned by emitter.listeners(event).

在添加 listener 时会发生该事件。 此时无法确定 listener 是否在 emitter.listeners(event) 返回的列表中。

事件: 'removeListener'#

  • event String The event name
  • listener Function The event handler function

  • event String 事件名

  • listener Function 事件处理函数

This event is emitted any time someone removes a listener. It is unspecified if listener is in the list returned by emitter.listeners(event).

在移除 listener 时会发生该事件。 此时无法确定 listener 是否在 emitter.listeners(event) 返回的列表中。