NodeEventTarget 对比 EventEmitter
NodeEventTarget
对象实现了 EventEmitter
API 的修改子集,允许它在某些情况下接近地模拟 EventEmitter
。
NodeEventTarget
不是 EventEmitter
的实例,在大多数情况下不能代替 EventEmitter
。
- 与
EventEmitter
不同,任何给定的listener
最多可以在每个事件type
中注册一次。 尝试多次注册listener
将被忽略。 NodeEventTarget
不模拟完整的EventEmitter
API。 特别是prependListener()
、prependOnceListener()
、rawListeners()
、setMaxListeners()
、getMaxListeners()
和errorMonitor
API 没有被模拟。'newListener'
和'removeListener'
事件也不会触发。NodeEventTarget
没有为类型为'error'
的事件实现任何特殊的默认行为。NodeEventTarget
支持EventListener
对象以及作为所有事件类型句柄的函数。
The NodeEventTarget
object implements a modified subset of the
EventEmitter
API that allows it to closely emulate an EventEmitter
in
certain situations. A NodeEventTarget
is not an instance of EventEmitter
and cannot be used in place of an EventEmitter
in most cases.
- Unlike
EventEmitter
, any givenlistener
can be registered at most once per eventtype
. Attempts to register alistener
multiple times are ignored. - The
NodeEventTarget
does not emulate the fullEventEmitter
API. Specifically theprependListener()
,prependOnceListener()
,rawListeners()
,setMaxListeners()
,getMaxListeners()
, anderrorMonitor
APIs are not emulated. The'newListener'
and'removeListener'
events will also not be emitted. - The
NodeEventTarget
does not implement any special default behavior for events with type'error'
. - The
NodeEventTarget
supportsEventListener
objects as well as functions as handlers for all event types.