Created
September 22, 2015 14:00
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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