簡體   English   中英

為什么要先注冊監聽器,然后在 node.js 中調用 emit 方法? 我們不能顛倒順序仍然可以工作嗎?

[英]why should one register listener first and call emit method next in node.js?? can't we reverse the order and still be able to work?

為什么聽者的順序應該在發聲體之上?

const EventEmitter=require('events');
const emitter=new EventEmitter();
   
 //Register a Listener
    
      emitter.on('messageLogged',function(){
        console.log('Listener Called');
        })
//Raise an event
    
    emitter.emit('messageLogged');

因為您需要在發出事件之前訂閱事件的偵聽器。

這個想法是,當您調用emit時,它將調用您附加到事件的函數。 如果沒有 function 它什么都不會調用。 您不能為您當前emit的事件調用將來附加的 function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM