Last active
March 10, 2016 03:45
-
-
Save harish2704/5d3d0195372fbd8487d4 to your computer and use it in GitHub Desktop.
Simple stupid Event Emitter class implementation in Javascript
This file contains 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
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