Why are globals bad?

后端 未结 5 1689
鱼传尺愫
鱼传尺愫 2020-11-29 11:54

It totaly makes sense to me to use it here. What would be the alternative? How can i generaly avoid to use them and most of all why is it bad according to jsLint to make us

5条回答
  •  误落风尘
    2020-11-29 13:00

    Globale variables could clash with other Scripts or be overwritten. When you don't need a global, it's advisable to avoid them. Simply use var (or let if your JS-Version-Support is greater than 1.7):

    (function() {
      var foo = 'bar';
      alert(foo);
    })();
    

提交回复
热议问题