Created
October 6, 2014 17:29
-
-
Save jiaaro/5947e338acb02d8a05df to your computer and use it in GitHub Desktop.
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 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