Why use closures? Do not use global variables?

I think it"s all about fear of [polluting] global variables. But VueX also declares a lot of global variables, why not be afraid at this time?

Mar.07,2022

A lot of things are not absolute, mainly for fear of misuse when others (especially beginners) don't understand.
in the beginner stage, we can abide by these norms, and if we want to be proficient, we need to understand why.
only if we know why, will we know when to use it and when not to use it.

such as global variables. The downside is that global variables will conflict and can easily be modified to cause errors, so it is more appropriate to use local variables when it is not unnecessary. The advantage is that it can be used in all scopes. When you understand the pros and cons of global variables, when you use them, you will consider whether to use them, whether there are alternatives, and if so, how to avoid the bad places, and the remedial measures after being destroyed. For example, there are conventions within the team to ensure that global variables are not overwritten and modified, and tests are used to ensure that they are not broken during the run.

for example, whether to add a semicolon or not, you are allowed to add a semicolon at first, because if you do not add a semicolon, some things will go wrong. For example, when the next line begins with a symbol such as [(+-/ ), it is basically the ASI mechanism . If you understand the principle, you can decide whether to add it or not. For example, if the company requires it to be added, it will be added, and if the rule is not added, it will not be added.

so it's not so absolute, it just depends on whether you understand why, use it if you know it, don't use it if you shouldn't, and learn to understand what you don't understand.


  1. regardless of packaging dependencies, vuex declares only one variable globally, that is, vuex or window.vuex .
  2. if many people in your company develop and an engineer uses a variable globally, is there a risk that other engineers will also use duplicate named variables? Then report a mistake, and you have to waste time looking for a problem, and in the end, an engineer may have to get a spurt.
Menu