Skip to content

Instantly share code, notes, and snippets.

@akkunchoi
Created February 16, 2014 16:37
Show Gist options
  • Select an option

  • Save akkunchoi/9036912 to your computer and use it in GitHub Desktop.

Select an option

Save akkunchoi/9036912 to your computer and use it in GitHub Desktop.
var EventEmitter = (function(){
var EventEmitter = function(){
this.listeners = {};
};
EventEmitter.prototype.emit = function(eventName){
var args = Array.prototype.slice.apply(arguments, [1]);
console.log(args);
if (this.listeners[eventName]){
for (var i = 0; i < this.listeners[eventName].length; i++){
this.listeners[eventName][i].apply(this, args);
}
}
};
EventEmitter.prototype.on = function(eventName, callback){
if (typeof this.listeners[eventName] === 'undefined'){
this.listeners[eventName] = [];
}
this.listeners[eventName].push(callback);
};
return EventEmitter;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment