1/** 2 * Subscriber 3 */ 4function Subscriber() { 5} 6 7Subscriber.prototype = { 8 receive: function(message) { 9 } 10} 11 12/** 13 * Publisher 14 */ 15function Publisher() { 16 this._subscribers = []; 17} 18 19Publisher.prototype = { 20 publish: function(message) { 21 for(var i=0; i<this._subscribers.length; i++) { 22 var subscriber = this._subscribers[i]; 23 subscriber.receive(message); 24 } 25 }, 26 add: function(subscriber) { 27 this._subscribers.push(subscriber); 28 } 29}