It's ok to add event ability to a constructor:
function Stream() {
events.EventEmitter.call(this);
}
util.inherits(Stream, events.EventEmitter);
But how to add event ability to an object instance? The following code doesn't work:
var foo = {x: 1, y: 2};
util.inherits(foo, event.EventEmitter);
events.EventEmitter.call(foo);
After running the above code, foo.on
still be undefined
.
Is there a way to inherit or mix-in EventEmitter's content?
You can use the npm module
node.extend
. It is a port of jQuery's$.extend
and with it, you can do this:or, if you don't want to use another module, you can use this: