events 事件触发器
源代码: lib/events.js
Node.js 的大部分核心 API 都是围绕惯用的异步事件驱动架构构建的,在该架构中,某些类型的对象(称为"触发器")触发命名事件,使 Function
对象("监听器")被调用。
例如:net.Server
对象在每次有连接时触发事件;fs.ReadStream
在打开文件时触发事件;流在每当有数据可供读取时触发事件。
所有触发事件的对象都是 EventEmitter
类的实例。
这些对象暴露了 eventEmitter.on()
函数,允许将一个或多个函数绑定到对象触发的命名事件。
通常,事件名称是驼峰式字符串,但也可以使用任何有效的 JavaScript 属性键。
当 EventEmitter
对象触发事件时,所有绑定到该特定事件的函数都会被同步地调用。
被调用的监听器返回的任何值都将被忽略和丢弃。
以下示例展示了使用单个监听器的简单的 EventEmitter
实例。
eventEmitter.on()
方法用于注册监听器,eventEmitter.emit()
方法用于触发事件。
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');
Source Code: lib/events.js
Much of the Node.js core API is built around an idiomatic asynchronous
event-driven architecture in which certain kinds of objects (called "emitters")
emit named events that cause Function
objects ("listeners") to be called.
For instance: a net.Server
object emits an event each time a peer
connects to it; a fs.ReadStream
emits an event when the file is opened;
a stream emits an event whenever data is available to be read.
All objects that emit events are instances of the EventEmitter
class. These
objects expose an eventEmitter.on()
function that allows one or more
functions to be attached to named events emitted by the object. Typically,
event names are camel-cased strings but any valid JavaScript property key
can be used.
When the EventEmitter
object emits an event, all of the functions attached
to that specific event are called synchronously. Any values returned by the
called listeners are ignored and discarded.
The following example shows a simple EventEmitter
instance with a single
listener. The eventEmitter.on()
method is used to register listeners, while
the eventEmitter.emit()
method is used to trigger the event.
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');