什么是定制eventemitters在node.js的目的是什么?

时间:2019-02-02 21:40:16

标签: javascript node.js function events

我了解onclick或onmosemove之类的事件,但是我发现自定义事件发射器的概念有些令人费解。我了解其运作方式。我不明白的是为什么有人会使用它而不是简单的功能。

例如:考虑下面的代码

var events = require('events');
var myEmmitter = new events.EventEmitter();

myEmmitter.on('anEvent',function(msg){
    console.log(msg);
});

myEmmitter.emit('anEvent','The event is absolutely emmited');

创建函数并在发出事件的地方调用它会不会更简单?如在下文中。

myEmitter = function(msg){
    cosole.log(msg);
}

myEmitter('the function is being called where it would have been emitted');

0 个答案:

没有答案