Skip to content

Instantly share code, notes, and snippets.

@jiaaro
Created October 6, 2014 17:29
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 jiaaro/5947e338acb02d8a05df to your computer and use it in GitHub Desktop.
Save jiaaro/5947e338acb02d8a05df to your computer and use it in GitHub Desktop.
function Event() {
this.listeners = [];
}
Event.prototype.subscribe = function(fn) {
this.listeners.push(fn);
}
Event.prototype.unsubscribe = function(fn) {
for (var i = 0; i < this.listeners.length; i++) {
if (this.listeners[i] === fn) {
this.listeners.splice(i, 1);
}
}
}
Event.prototype.emit = function() {
for (var i = 0; i < this.listeners.length; i++) {
this.listeners[i].apply(this, arguments);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment