जटिल जेएस एप्लिकेशन बनाते समय, वैश्विक पर्यवेक्षक ऑब्जेक्ट का उपयोग करने के पेशेवर और विपक्ष क्या हैं जो घटनाओं को फायर करते हैं और अन्य सभी ऑब्जेक्ट्स बनाम मिश्रण या प्रोटोटाइप के लिए सदस्यता लेते हैं सभी वस्तुओं पर पब/उप विधियां जो अपनी घटनाओं को ट्रिगर करने के लिए ज़िम्मेदार हैं? उदाहरण के लिएवैश्विक पर्यवेक्षक ऑब्जेक्ट बनाम एक मिक्सिन
लें एक कार्ड खेल है, जिसमें व्यापारी, खिलाड़ी, और तालिका वस्तुओं (psuedocode-ish इस प्रकार) है:
// "Global" observer version
var observer = {
// publish and subscribe methods defined here
};
dealer.deal = function(cards) {
// performs logic for dealing cards
observer.publish('dealer:dealt', cards, this);
};
player.play = function(cards) {
// performs logic for which card is played
observer.publish('player:played', cards, this);
};
table.showCards = function(cards, player) {
// performs logic for showing cards that the dealer dealt
// or that the player played
};
observer.subscribe('dealer:dealt', table.showCards);
observer.subscribe('player:played', table.showCards);
बनाम
// Pub/sub mixin/prototype version
dealer.deal = function(cards) {
// performs logic for dealing cards
this.publish('dealt', cards);
};
player.play = function(cards) {
// performs logic for which card is played
this.publish('played', cards);
};
table.showCards = function(cards) {
// performs logic for showing cards that the dealer dealt
// or that the player played
};
dealer.subscribe('dealt', table.showCards);
player.subscribe('played', table.showCards);
आप किस तरह से खत्म हो गए? मैं वर्तमान में एक ही निर्णय के साथ जुड़ा हुआ हूँ। और सवाल उठाने के एक साल बाद भी चुनाव करने में मदद करने के लिए वहां कोई स्पष्ट डेटा नहीं दिखता है। ग्लोबल भी पूल अधिक मुक्त और कम स्मृति गहन लगते हैं, लेकिन जब सिस्टम वास्तव में बहुत बड़ा हो रहा है तो मुझे आश्चर्य है कि यह केवल अप्रत्याशित रूप से जटिल हो जाएगा। – nicholas