Last active
August 29, 2015 14:05
-
-
Save sTiLL-iLL/159bb55d8ac3cf4356fc to your computer and use it in GitHub Desktop.
An example of inheriting from event emitter
This file contains hidden or 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
// Require our emitter | |
var Emitter = require('events').EventEmitter; | |
// Our main constructor | |
var myEmitter = function (config) { | |
// extend with emitter | |
Emitter.call(this); | |
}; | |
// Inherit from emitter, but keep my constructor | |
myEmitter.prototype = Object.create(Emitter.prototype, { | |
constructor: { | |
value: myEmitter | |
} | |
}); | |
// methods | |
myEmitter.prototype.setName = function (nam) { | |
this.name = nam; | |
this.emit('nameChanged', name); | |
}; | |
module.exports = myEmitter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment