emitter.on(eventName, listener)
eventName
<string> | <symbol> 事件的名称。listener
<Function> 回调函数- 返回: <EventEmitter>
将 listener
函数添加到名为 eventName
的事件的监听器数组的末尾。
不检查是否已添加 listener
。
多次调用传入相同的 eventName
和 listener
组合将导致多次添加和调用 listener
。
server.on('connection', (stream) => {
console.log('someone connected!');
});
返回对 EventEmitter
的引用,以便可以链式调用。
默认情况下,事件监听器按添加顺序调用。
emitter.prependListener()
方法可用作将事件监听器添加到监听器数组开头的替代方法。
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// 打印:
// b
// a
eventName
<string> | <symbol> The name of the event.listener
<Function> The callback function- Returns: <EventEmitter>
Adds the listener
function to the end of the listeners array for the
event named eventName
. No checks are made to see if the listener
has
already been added. Multiple calls passing the same combination of eventName
and listener
will result in the listener
being added, and called, multiple
times.
server.on('connection', (stream) => {
console.log('someone connected!');
});
Returns a reference to the EventEmitter
, so that calls can be chained.
By default, event listeners are invoked in the order they are added. The
emitter.prependListener()
method can be used as an alternative to add the
event listener to the beginning of the listeners array.
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a