When creating a complex JS application, what are the pros and cons of a global observer object that fires events and which all other objects subscribe, or by mixing or prototyping the pub / helper methods on all objects that are responsible for triggering their own events?
Take, for example, a card game with trading, game, and table objects (psuedocode-ish):
var observer = {
};
dealer.deal = function(cards) {
observer.publish('dealer:dealt', cards, this);
};
player.play = function(cards) {
observer.publish('player:played', cards, this);
};
table.showCards = function(cards, player) {
};
observer.subscribe('dealer:dealt', table.showCards);
observer.subscribe('player:played', table.showCards);
against
dealer.deal = function(cards) {
this.publish('dealt', cards);
};
player.play = function(cards) {
this.publish('played', cards);
};
table.showCards = function(cards) {
};
dealer.subscribe('dealt', table.showCards);
player.subscribe('played', table.showCards);
source
share