当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Node.js EventEmitter emitter.rawListeners(eventName)用法及代码示例

emitter.rawListeners(eventName)

添加于:v9.4.0

参数

返回名为 eventName 的事件的侦听器数组的副本,包括任何包装器(例如由 .once() 创建的包装器)。

const emitter = new EventEmitter();
emitter.once('log', () => console.log('log once'));

// Returns a new Array with a function `onceWrapper` which has a property
// `listener` which contains the original listener bound above
const listeners = emitter.rawListeners('log');
const logFnWrapper = listeners[0];

// Logs "log once" to the console and does not unbind the `once` event
logFnWrapper.listener();

// Logs "log once" to the console and removes the listener
logFnWrapper();

emitter.on('log', () => console.log('log persistently'));
// Will return a new Array with a single function bound by `.on()` above
const newListeners = emitter.rawListeners('log');

// Logs "log persistently" twice
newListeners[0]();
emitter.emit('log');

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 emitter.rawListeners(eventName)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。