I've Heard Global Variables Are Bad, What Alternative Solution Should I Use?

后端 未结 9 857
暖寄归人
暖寄归人 2020-11-22 03:26

I\'ve read all over the place that global variables are bad and alternatives should be used. In Javascript specifically, what solution should I choose.

I\'m thinking

9条回答
  •  猫巷女王i
    2020-11-22 03:57

    You really don't want to do this.
    As to why, see e.g. the top post here: What is the most EVIL code you have ever seen in a production enterprise environment?

    As a side note, one can always execute "global" code without littering the place with globals:

    (function () {
        var notaglobal = 1;
        alert(notaglobal);
    })();
    //notaglobal is not defined in this scope        
    

提交回复
热议问题