Skip to content

Instantly share code, notes, and snippets.

@harish2704
Last active March 10, 2016 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harish2704/5d3d0195372fbd8487d4 to your computer and use it in GitHub Desktop.
Save harish2704/5d3d0195372fbd8487d4 to your computer and use it in GitHub Desktop.
Simple stupid Event Emitter class implementation in Javascript
function EventEmitter(){
this.listers = {};
}
EventEmitter.prototype = {
on: function( name, handler ){
var evnt = this.listers[ name ];
if( !evnt ){
this.listers[ name ] = [ handler ];
} else {
evnt.push( handler );
}
},
emit: function( name, data1, data2 ){
var evnt = this.listers[ name ];
if( evnt ){
evnt.forEach( function( handler ){
handler( name, data1, data2 );
});
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment