Pub/Sub (Dustin Diaz's)
See Diaz’s article.
Head over to remy sharp’s JS Bin and play.
function Publisher(name) { this.name = name; this.subscribers = []; } Publisher.prototype.deliver = function (data, scope) { scope = scope || window; var self = this; this.subscribers.forEach( function (value) { value.call(scope, self.name, data); } ); return this; }; Function.prototype.subscribe = function (publisher) { var self = this, isExistent = publisher.subscribers.some( function (value) { return (value === self); } ); if (!isExistent) { publisher.subscribers.push(this); } return this; }; Function.prototype.unsubscribe = function (publisher) { var self = this; publisher.subscribers = publisher.subscribers.filter( function(value) { if (value !== self) { return value; } } ); return this; };