eventemit.js
478 Bytes
function Emitter(){
this.events = {};
}
Emitter.prototype.on = function(type, listener){
this.events[type] = this.events[type] || [];
this.events[type].push(listener);
}
const emitter = new Emitter();
emitter.on('soccer', function(){
console.log('soccer logged.');
})
Emitter.prototype.emit = function(type){
if(this.events[type]){
this.events[type].forEach(function(listener){
listener();
});
}
}
emitter.emit('soccer');