节点的模块度_节点事件模块

节点的模块度

The events module provides us the EventEmitter class, which is key to working with events in Node.

events模块为我们提供了EventEmitter类,这是在Node中处理事件的关键。

I published a full article on that, so here I will just describe the API without further examples on how to use it.

我对此发表了一篇完整的文章 ,因此在这里我将仅描述该API,而无需进一步说明如何使用它。

const EventEmitter = require('events')
const door = new EventEmitter()

The event listener eats its own dog food and uses these events:

事件侦听器吃自己的狗食并使用以下事件:

  • newListener when a listener is added

    添加侦听newListener时的newListener

  • removeListener when a listener is removed

    移除监听器后的removeListener

Here’s a detailed description of the most useful methods:

这是最有用的方法的详细说明:

emitter.addListener() (emitter.addListener())

Alias for emitter.on().

emitter.on()别名。

emitter.emit() (emitter.emit())

Emits an event. It synchronously calls every event listener in the order they were registered.

发出事件。 它按照注册事件的顺序同步调用每个事件侦听器。

emitter.eventNames() (emitter.eventNames())

Return an array of strings that represent the events registered on the current EventListener:

返回一个字符串数组,这些字符串表示在当前EventListener上注册的事件:

door.eventNames()

emitter.getMaxListeners() (emitter.getMaxListeners())

Get the maximum amount of listeners one can add to an EventListener object, which defaults to 10 but can be increased or lowered by using setMaxListeners()

获取可以添加到EventListener对象的最大侦听器数量,该侦听器默认为10,但可以使用setMaxListeners()进行增加或降低

door.getMaxListeners()

emitter.listenerCount() (emitter.listenerCount())

Get the count of listeners of the event passed as parameter:

获取作为参数传递的事件的侦听器计数:

door.listenerCount('open')

emitter.listeners() (emitter.listeners())

Gets an array of listeners of the event passed as parameter:

获取作为参数传递的事件的侦听器数组:

door.listeners('open')

emitter.off() (emitter.off())

Alias for emitter.removeListener() added in Node 10

在节点10中添加了emitter.removeListener()别名

emitter.on() (emitter.on())

Adds a callback function that’s called when an event is emitted.

添加发出事件时调用的回调函数。

Usage:

用法:

door.on('open', () => {
  console.log('Door was opened')
})

emitter.once() (emitter.once())

Adds a callback function that’s called when an event is emitted for the first time after registering this. This callback is only going to be called once, never again.

添加一个回调函数,该函数在注册此事件后首次发出事件时被调用。 该回调只会被调用一次,不会再被调用。

const EventEmitter = require('events')
const ee = new EventEmitter()

ee.once('my-event', () => {
  //call callback function once
})

emitter.prependListener() (emitter.prependListener())

When you add a listener using on or addListener, it’s added last in the queue of listeners, and called last. Using prependListener it’s added, and called, before other listeners.

使用onaddListener添加侦听器时on该侦听器被添加到侦听器队列的最后,并被称为last。 使用prependListener可以在其他侦听器之前添加并调用它。

emitter.prependOnceListener() (emitter.prependOnceListener())

When you add a listener using once, it’s added last in the queue of listeners, and called last. Using prependOnceListener it’s added, and called, before other listeners.

当您使用once添加侦听器once ,它会被添加到侦听器队列中的最后一个,并被称为最后一个。 使用prependOnceListener可以在其他侦听器之前添加并调用它。

emitter.removeAllListeners() (emitter.removeAllListeners())

Removes all listeners of an event emitter object listening to a specific event:

删除事件发射器对象的所有侦听特定事件的侦听器:

door.removeAllListeners('open')

emitter.removeListener() (emitter.removeListener())

Remove a specific listener. You can do this by saving the callback function to a variable, when added, so you can reference it later:

删除特定的侦听器。 您可以通过将回调函数保存到变量中(添加后)来完成此操作,以便以后可以引用它:

const doSomething = () => {}
door.on('open', doSomething)
door.removeListener('open', doSomething)

emitter.setMaxListeners() (emitter.setMaxListeners())

Sets the maximum amount of listeners one can add to an EventListener object, which defaults to 10 but can be increased or lowered.

设置一个可以添加到EventListener对象的侦听器的最大数量,该侦听器默认为10,但可以增加或降低。

door.setMaxListeners(50)

翻译自: https://flaviocopes.com/node-module-events/

节点的模块度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值