Skip to content

Instantly share code, notes, and snippets.

@markspereira
Created September 26, 2016 11:23
Show Gist options
  • Save markspereira/7b5af3c25b51c6b9adc781322aac6c1c to your computer and use it in GitHub Desktop.
Save markspereira/7b5af3c25b51c6b9adc781322aac6c1c to your computer and use it in GitHub Desktop.
//index.js
var Emitter = require('./emitter');
var emtr = new Emitter();
emtr.on('greet', function(){
console.log('Hello 1');
});
emtr.on('greet', function(){
console.log('Hello 2');
});
console.log('Hello 3');
emtr.emit('greet');
//emitter.js
function Emitter(){
this.events = {};
}
Emitter.prototype.on = function(type, listener){
this.events[type] = this.events[type] || [];
this.events[type].push(listener);
}
Emitter.prototype.emit = function(type, listener) {
if (this.events[type]) {
this.events[type].forEach(function(listener) {
listener();
});
}
}
Emitter.prototype.emit = function(type){
if (this.events[type]) {
this.events[type].forEach(function(listener){
listener();
});
}
}
module.exports = Emitter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment