0

Possible Duplicate:
Simplest/Cleanest way to implement singleton in JavaScript?

Here is a code snippest that tries to implement a singleton object.

Game.Score = (

    function () {
        ....
        var currentScorePolicy = {
            onBrickHitCorrectBasket: function () { alert("defaultScorePolicy::onBrickHitCorrectBasket called"); },
            onBrickHitWrongBasket: function () { },
            onBrickMissAllBaskets: function () { }
        };
        ...
        return {
             ....
             applyScorePolicyWithBonusEnabled: function (scorePerQuestion, maxBonusFactor) {
                currentScorePolicy = {
                    onBrickHitCorrectBasket: function () {
                       // alert("applyScorePolicyWithBonusEnabled::onBrickHitCorrectBasket called");
                        raise();
                        enlargeBonus();

                    },
                    onBrickHitWrongBasket: function () {
                        reduce();
                        resetBonus();
                    },
                    onBrickMissAllBaskets: function () { }
                };

               // alert("applyScorePolicyWithBonusEnabled called");
                that.currentScorePolicy.onBrickHitCorrectBasket();
            },

            applyScorePolicyWithBonusDisabled: function (scorePerQuestion) {
                currentScorePolicy = {
                    onBrickHitCorrectBasket: function () {
                        raise(scorePerQuestion); 
                    },
                    onBrickHitWrongBasket: function () {
                        reduce(scorePerQuestion);
                    },
                    onBrickMissAllBaskets: function () { }
                };
            },

            onBrickHitCorrectBasket: currentScorePolicy.onBrickHitCorrectBasket,
            onBrickHitWrongBasket: currentScorePolicy.onBrickHitWrongBasket,
            onBrickMissAllBaskets: currentScorePolicy.onBrickMissAllBaskets

        }

    })();

It's a score object for my game when the scoring policy may change during a game. The Score object expose two methods: applyScorePolicyWithBonusEnabled and applyScorePolicyWithBonusDisabled, thier goal is to alter the currentScorePolicy field.

Now, when I start my app I initialize Score and call applyScorePolicyWithBonusEnabled. Later on I call Score.onBrickHitCorrectBasket expecting that ScorePolicyWithBonus.onBrickHitCorrectBasket will execute, but in fact what is running is the original stub currentScorePolicy.onBrickHitCorrectBasket is called. I can tell by the alerts calls I planted there.

What's wrong? why does the call applyScorePolicyWithBonusEnabled() did not assigned the new object to currentScorePolicy?

Community
  • 1
  • 1
Ron Zukerman
  • 61
  • 1
  • 8
  • Looks like you're changing `currentScorePolicy` in the closure of the singleton, but then explicitly calling an instance with `that.currentScorePolicy`. Changing the local `var` will not update the `that` reference as well. If they point to the same object (reference) and you change properties on them, then the local var and the `that` version will update together. Anyway, I'm not sure if this is intended or not, which is why I'm commenting and not providing a possible solution. – Eli Gassert Nov 12 '12 at 13:33
  • 1
    The singleton pattern is a workaround to create single objects in a pure class-based language (eg Java). It does not make sense in JavaScript at all, because in JavaScript you can just create single objects directly. – Deestan Nov 12 '12 at 13:34

0 Answers0