當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。