Event Emitter

by Abhisek DasGupta

JavaScript

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){
	if(this.events[type]){
  	this.events[type].forEach(function(listener){
    	listener();
    });
  }
}

var emtr = new Emitter();
console.log(emtr);

emtr.on('GREET',function(){
	console.log("Hello I am here !!!");
});

emtr.on('GREET',function(){
	console.log("Secong one hello!!!");
});

emtr.emit('GREET1');