Publisher.prototype.unsubscribe = function (token) { for (var m inthis.topics) { if (this.topics[m]) { for (var i = 0, j = this.topics[m].length; i < j; i++) { if (this.topics[m][i].token === token) { this.topics[m].splice(i, 1); return token; } } } } returnthis; };
var Subscriber = function (name) { this.name = name; };
Subscriber.prototype.log = function (topic, message) { console.log( "I am " + this.name + ", I receive message from " + topic + ", the message is: " + message ); };
var publisher = new Publisher();
var subscriber1 = new Subscriber("subscriber1"); var subscriber2 = new Subscriber("subscriber2"); var subscriber3 = new Subscriber("subscriber3"); var subscriber4 = new Subscriber("subscriber4"); var subscriber5 = new Subscriber("subscriber5");
publisher.publish("channer1", "channer1 is big"); publisher.publish("channer2", "channer2 is not big"); publisher.publish("channer3", "channer3 is small");