A JavaScript variable declaration statement, regardless of where it appears, is first executed before other code. The scope of declaring a variable with the VAR keyword is the current execution context, possibly a peripheral function, or a global variable when the variable is declared outside the body of the function.
defined in the body of the function are global variables, defined in the body of the function of local variables. The definition here refers to the Var declaration.
JavaScript has an implied global concept, meaning that any variable you don't declare becomes a global object attribute. For example:
function test () {
myname = "huming";
alert (myname);
}
Test (); "Huming"
Two results are the same, stating that myname is a global variable.
So, is there any difference between an implicit global variable and a well-defined global variable? The answer must be yes, look at the following example:
Defines three global variables
var global_test =;
Global_test =; Negative example
(function () {
global_test =;//Negative Example
} ());
An attempt was made to delete
the delete global_test;//False delete global_test;//True delete
global_test;//True
//test the deletion
alert (typeof global_test);//"Number"
alert (typeof global_test);//"Undefined"
As can be seen from the example above, the global_test1 defined by Var outside the function cannot be deleted, and Global_test2 and GLOBAL_TEST3, which are not defined by Var, are removed (whether they are created in the function body).
In summary, global variables declared by var outside the function body cannot be deleted, and implicit global variables can be deleted.
Note here: JavaScript has a behavior called "hoisting" (Mount/Top parsing/Pre-resolution).
We illustrate by an example:
var myname = "Huming"; Declare global variable
function test () {
alert (myname);
var myname = "local_huming";
alert (myname);
}
Test ();
Do you guess two times alert's content is consistent?? Clearly inconsistent, consistent still used to say? The actual output is: "Undefined", "local_huming".
The above example is equivalent to
var myname = "Huming"; Declare global variable
function test () {
var myname;
Alert (maname);<br> myname = "local_huming";
alert (myname); "Local"
}
The myname of the first alert output is not what you think of as a global variable, but the local variable that is in a scope (a function body). Although it has not yet been declared, it is regarded as a statement. This is the so-called "hoisting".
This should be understood. When you use a variable in the body of a function and then declare it again, you may have an error.
Writing specification:
function test () {
var a =,
B =,
c = a + B,
d = {},
E,
F;
function body ...
}
The advantage is:
1, all local variables are defined at the beginning of the function, easy to find;
2. Prevent variables from using logic errors before they are defined.
In JavaScript, there are four ways in which a variable name (name) enters scope (scope)
Language built in, all scopes have this and arguments keywords
Formal arguments, the parameters of a function are valid throughout the scope
function declaration
Variable declaration
The four order listed above is also the order of the highest priority, and once a variable name has been declared, it cannot be overwritten by other lower-priority variable declaration forms.