我突然意识到NodeJS中的事件发射器通常就像Java中的静态方法。示例:
// This illustrated that event listener is universal
function A(a){
var that = this;
this.a = a;
this.cnt = 0;
this.done = function(){
this.emit("done");
};
this.say = function(){
console.log(a + " = " + that.cnt);
};
this.inc = function(){
that.cnt++;
};
}
A.prototype = new events.EventEmitter;
var a = new A("a"),
b = new A("b"),
c = new A("c");
a.on("done",function(){a.inc()});
b.on("done",function(){b.inc()});
c.on("done",function(){c.inc()});
c.done();
c.done();
a.say();
b.say();
此代码将给出输出:
a = 2
b = 2
虽然我实际上期待:
a = 0
b = 0
我相信这是因为这条线:
A.prototype = new events.EventEmitter;
我认为“原型”类型的定义会像 Java 中的“静态”一样使用。
为了拥有基于每个对象的事件侦听器,我将上面的代码更改为:
function B(a){
var that = this;
this.evt = new events.EventEmitter;
this.a = a;
this.cnt = 0;
this.done = function(){
this.evt.emit("done");
};
this.say = function(){
console.log(a + " = " + that.cnt);
};
this.inc = function(){
that.cnt++;
};
}
var a = new B("a"),
b = new B("b"),
c = new B("c");
a.evt.on("done",function(){a.inc()});
b.evt.on("done",function(){b.inc()});
c.evt.on("done",function(){c.inc()});
c.done();
c.done();
a.say();
b.say();
这将是每个对象的事件侦听器,但我并不认为这是一个好的设计/实现,因为它破坏了 EventEmitter 的链接。即,像下面的代码:
// can chain another method of A after the on() method
a.on("event",functionCallback).anotherMethodOfA();
我想问一下,NodeJS 中每个对象的事件监听器的正确实现是什么?