Skip to content

Instantly share code, notes, and snippets.

@NeoTech
Created September 22, 2015 14:00
Show Gist options
  • Save NeoTech/4bf5d0e2b484abd99ed6 to your computer and use it in GitHub Desktop.
Save NeoTech/4bf5d0e2b484abd99ed6 to your computer and use it in GitHub Desktop.
How to handle event emitters in Node.JS and a common prototype pattern for it.
var EventEmitter = require('events').EventEmitter;
function EventProto() {
if (!(this instanceof EventProto)) {
console.log('Not an instance; returning new.');
return new EventProto();
}
EventEmitter.call(this);
var _self = this;
}
EventProto.prototype = Object.create(EventEmitter.prototype);
EventProto.prototype.SayHello = function() {
this.emit('SayHello','Hello');
};
var e = new EventProto();
e.on('SayHello', function(evt) {
console.log(evt);
});
e.SayHello();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment