Node.js 发射器.eventNames() 方法
在 Node.js 中,大多数核心 API 都是围绕惯用的异步和事件驱动架构构建的。 EventEmitter 类具有由所有对象作为事件发出的实例,其中这些对象公开了一个eventEmitter.on()函数。必须导入事件库( require('events') )才能处理事件。
发射器.eventNames() (在v6.0.0中添加)方法是事件模块的内置应用程序编程接口,用于列出发射器已注册监听器的事件并以数组形式返回。这些侦听器的类型是字符串或符号。
句法:
const EventEmitter = require('events');
emitter.eventNames()
参数:此函数不接受任何参数。
Return Value < Array > :它返回一个包含字符串或符号值的数组,列出发射器已注册侦听器的事件。
示例 1:文件名:index.js
// Node.js program to demonstrate the
// emitter.eventNames() method
// Using require to access events module
const EventEmitter = require('events');
// Creating new event Emitter
const newEventEmitter = new EventEmitter();
// Creating new event akash
newEventEmitter.on('akash', () => {});
// Creating new event hadii
newEventEmitter.on('hadii', function() {});
// Error monitor function
console.log(EventEmitter.errorMonitor);
// Creating new symbol event newSymbol
const newSym = Symbol('newSymbolofGeekyWorld');
newEventEmitter.on(newSym, () => {});
// Printing event Names
console.log(newEventEmitter.eventNames());
// Prints: [ 'akash', 'hadii',
// Symbol(newSymbolofGeekyWorld) ]
输出:
Symbol(events.errorMonitor)
[ ‘akash’, ‘hadii’, Symbol(newSymbolofGeekyWorld) ]
示例 2:文件名:index.js
// Node.js program to demonstrate the
// emitter.eventNames() method
// Using require to access events module
const EventEmitter = require('events');
// Creating new event Emitter
const newEventEmitter = new EventEmitter();
// Creating a new callback function
const someCallback = () => {
console.log('Connected to something!');
};
// Creating new event connectSomething
newEventEmitter.on('connectSomething', someCallback);
// Creating new event alfa
newEventEmitter.prependListener('alfa', () => console.log('b'));
// Creating new event alfa, not added to eventnames array...
newEventEmitter.once('geeksforgeeks',
() => console.log('print in console...'));
// Creating new event alfa, not added to eventnames array...
newEventEmitter.prependOnceListener('geeksforgeeks',
() => console.log('geeksforgeeks'));
// Emitting geeksforgeeks event
newEventEmitter.emit('geeksforgeeks');
// Creating new symbol event newSymbolofGeekyWorld
const newSym = Symbol('newSymbolofGeekyWorld');
newEventEmitter.on(newSym, () => {});
// Printing event Names
console.log(newEventEmitter.eventNames());
// Removing connectSomething event
newEventEmitter.removeListener('connectSomething', someCallback);
// Printing event Names
console.log(newEventEmitter.eventNames());
使用以下命令运行index.js文件:
node index.js
输出:
geeksforgeeks
print in console…
[ ‘connectSomething’, ‘alfa’, Symbol(newSymbolofGeekyWorld) ]
[ ‘alfa’, Symbol(newSymbolofGeekyWorld) ]
参考: https://nodejs.org/api/events.html#events_emitter_eventnames