events.listenerCount(emitter, eventName)
稳定性: 0 - 弃用: 改为使用
emitter.listenerCount()
。emitter
<EventEmitter> 要查询的触发器eventName
<string> | <symbol> 事件名称
返回在给定 emitter
上注册的给定 eventName
的监听器数量的类方法。
import { EventEmitter, listenerCount } from 'node:events';
const myEmitter = new EventEmitter();
myEmitter.on('event', () => {});
myEmitter.on('event', () => {});
console.log(listenerCount(myEmitter, 'event'));
// 打印: 2
const { EventEmitter, listenerCount } = require('node:events');
const myEmitter = new EventEmitter();
myEmitter.on('event', () => {});
myEmitter.on('event', () => {});
console.log(listenerCount(myEmitter, 'event'));
// 打印: 2
Stability: 0 - Deprecated: Use
emitter.listenerCount()
instead.emitter
<EventEmitter> The emitter to queryeventName
<string> | <symbol> The event name
A class method that returns the number of listeners for the given eventName
registered on the given emitter
.
import { EventEmitter, listenerCount } from 'node:events';
const myEmitter = new EventEmitter();
myEmitter.on('event', () => {});
myEmitter.on('event', () => {});
console.log(listenerCount(myEmitter, 'event'));
// Prints: 2
const { EventEmitter, listenerCount } = require('node:events');
const myEmitter = new EventEmitter();
myEmitter.on('event', () => {});
myEmitter.on('event', () => {});
console.log(listenerCount(myEmitter, 'event'));
// Prints: 2