Created
October 20, 2012 16:30
-
-
Save brentertz/3923852 to your computer and use it in GitHub Desktop.
Node.js: Extend a class to be an EventEmitter
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 util = require('util'), | |
EventEmitter = require('events').EventEmitter; | |
var Server = function() { | |
var self = this; | |
this.on('custom_event', function() { | |
self.logSomething('custom_event'); | |
}); | |
this.logSomething('init'); | |
}; | |
util.inherits(Server, EventEmitter); | |
Server.prototype.doSomething = function() { | |
this.emit('custom_event'); | |
}; | |
Server.prototype.logSomething = function(something) { | |
console.log(something); | |
} | |
var s = new Server(); | |
s.doSomething(); |
stephanelpaul
commented
Apr 16, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment