Let's look at a piece of code first.
Function Show () {
alert (ABC);
}
var abc= "DEFG";
Show ();
People who have experience in C + + or Java programming may say, "This program is dead, and the variable is defined behind the function that references the variable, and the bug will kill you." "Put it on the browser and run it, what's the result?" Perfect run! So let's just say what this is about--there's a difference between VAR and a variable that doesn't have a var definition.
1, no Var
To be concise, it is unsafe to omit Var when defining a variable, but it is legal. At this point, regardless of where the variable is defined, the interpreter gives the variable a global scope.
2, there are Var
Safe, lawful. The scope of the defined variable depends on where it is defined. As for what the scope is, see the "JavaScript scope" article in this blog.
In this way, the beginning of the problem can be solved. In the function is the definition of ABC, but the value is undefined, then ABC has a global scope, the function is only the value of the ABC update.