Observer with topics
Topics in the observer pattern contains more than one observable objects. Normally, it can happen that we need to create multiple observable objects in some situation. In that case, it is better that we use topic functionality of the observer pattern. Example of topics in the observer pattern is as follows:
var Observable = { users: [], addUser: function(item, observer) { this.users[item] || (this.observers[topic] = []) this.users[item].push(observer) }, removeUser: function(item, observer) { if (!this.users[item]) return; var index = this.users[item].indexOf(observer) if (~index) { this.users[item].splice(index, 1) } }, notifyUsers: function(item, note) { if (!this.users[item]) return; for (var i = this.users[item].length - 1; i >= 0; i--) { this.users[item][i](note) }; } } Observable.addUser('bag', function(note){ console.log("First...