Skip to content

Instantly share code, notes, and snippets.

@mertcetin
Created March 6, 2014 20:58
Show Gist options
  • Save mertcetin/9399498 to your computer and use it in GitHub Desktop.
Save mertcetin/9399498 to your computer and use it in GitHub Desktop.
function Event() {
var subscribers = [];
this.subscribe = function(args) {
for (var i=0; i < arguments.length; i++) {
if (typeof(arguments[i]) === 'function'){
subscribers.push(arguments[i]);
}
}
};
this.unsubscribe = function(args) {
for (var i=0; i < arguments.length; i++) {
if (typeof(arguments[i]) === 'function') {
for (var y = subscribers.length; y > 0; --y) {
if (subscribers[y] === arguments[i]) {
subscribers.splice(y,1);
break;
}
}
}
}
};
this.emit = function(args) {
var that = this;
var copy = [].slice.call(arguments);
subscribers.forEach(function(subscriber){
subscriber.apply(that, copy);
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment