Skip to content

Instantly share code, notes, and snippets.

@jiaaro jiaaro/solution.js
Created Oct 6, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.