0

I defined a module (for the code easy to read,I delete some unnecessary line..)

Here is my game.js

define(['util'], function (util) {
    return {
        createNew : function (setting) {
            var game = {
                intervalTrigger : function () {
                    return window.setInterval(function () {
                        score++;//how to read deal with this value?
                    }, 1000);
                }
            }
        };
        return game;
    }
};
});

and use it in app.js:

require.config({
    baseUrl: "./src",
});

requirejs(['util', 'game'],
    function (util,game) {
        var score = 0;

        function startgame(level) {
            var setting = {};
            var g = game.createNew();
            g.intervalTrigger();
        }
        startgame(0);
    });

in my case, I need to createNew for few times, so the score can't be stored in the module.

so how to access score in the module game?

chanjianyi
  • 607
  • 4
  • 15
  • 35

2 Answers2

0

create a get/set function and expose it in the game.js:

define(['util'], function (util) {
    var score = 0;
    return {
        createNew : function (setting) {
            var game = {

                intervalTrigger : function () {
                    return window.setInterval(function () {                     
                      score++;//how to read deal with this value?
                    }, 1000);
                }
                getScore: function(){ return score;} //getting the score
                setScore: function(value){score = value;} //setting the score
                }

            };
            return game;

        }
    };
});

now here you can use:

        require.config({
          baseUrl: "./src",
    });
requirejs(['util', 'game'],
        function   (util,game) {
            var score = 0;

            function startgame(level) {
                var setting = {};
                var g = game.createNew();
                g.setScore(score); //setting the score
                g.intervalTrigger();
            }
            startgame(0);
     });
Saar
  • 2,276
  • 1
  • 16
  • 14
  • wouldn't be better to define score as an actual getter/setter and access it this way? `this.score++`? – Matías Fidemraizer Sep 14 '15 at 07:47
  • not if you want to have validations on the set method – Saar Sep 14 '15 at 08:31
  • Hi Matias, I know about them just didn't think about it when gave the answer :) – Saar Sep 14 '15 at 11:01
  • Uhmmmmmmmmmmmmmmmmmmmmmmmmm – Matías Fidemraizer Sep 14 '15 at 11:02
  • I doubt it. Check your statement: **not if you want to have validations on the set method**... Humility is a plus here :( – Matías Fidemraizer Sep 14 '15 at 11:03
  • omg you are pathetic.. you exactly showed that you have no humility. yes ecma script 5 getters and setters are so over my head.. I don't know what they are, please teach me master. how do I use defineProperty – Saar Sep 14 '15 at 11:08
  • So why you say "not if you want to have validations on set method" if you know that you can define a setter.............................. – Matías Fidemraizer Sep 14 '15 at 11:14
  • Nevermind, if you already knew it who knows why you're still using getters/setters as regular functions (you might argue that you do for older browsers...). BTW, again, nevermind. – Matías Fidemraizer Sep 14 '15 at 11:16
  • Have you downvoted my answer as a vengance to my comments? ;O – Matías Fidemraizer Sep 14 '15 at 11:16
  • I've double-checked your "I'm patethic" statement, and it seems like I think that humility is still important in your case. Do I need to teach `Object.defineProperty`? Who said you need this? Your `getScore: function(){ return score;} //getting the score` can be turned into `get score() { return score; }`. I don't see an `Object.defineProperty`!!!!!! – Matías Fidemraizer Sep 16 '15 at 14:08
  • In the other hand, I've checked that both downvotes here on my answer and on http://stackoverflow.com/questions/4943817/mapping-object-to-dictionary-and-vice-versa/4944547#4944547 where made in the same minute and after some moments you added the "omg you're pathetic" comment. At least, I've not downvoted your answer and I've tried to suggest you how to improve your answer. Anyway, I'm not here for the points, I love contributing with content, that's all. – Matías Fidemraizer Sep 16 '15 at 14:10
0

You need to define score as an actual module. For example:

define([], function() {
      var gameStats = {
           score: 0
      };

      return gameStats;
});

Now you just need to inject wherever you need to work with game stats.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206