[英]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.