See example below. I am trying to trigger an event on a
.
var events = require("events");
function foo() {
this.on("ping", function() {
console.log("bar!");
})
}
foo.prototype = new events.EventEmitter();
var a = new foo();
var b = new foo();
a.emit("ping");
This prints "bar!" two times, so I am assuming I am adding the event listener on "all" the functions, not the specific instance. Since I am running foo two times, I am adding two event listeners, nothing strange really, althought not so intuitive.
Is there a way to add the event listener only on a
?
(Please edit this question if I am using the wrong terminology, used to class-based programming)
edit: So I suspect it is because I am using the same event emitter as prototype.. But in that case, how can I create one for each new foo()?