I. Javascript variable Declaration
Declare a variable in Javascript
Var a = 1;
Or directly
A = 1;
The two expressions are different,
One is the local variable of the current scope, and the other is the global variable of the current scope;
The special feature of Javascript is that the function can directly read global variables.
Copy codeThe Code is as follows:
Var n = 999;
Function f1 (){
Alert (n );
}
F1 (); // 999.
On the other hand, local variables in the function cannot be read outside the function.
Copy codeThe Code is as follows:
Function f1 (){
Var n = 999;
}
Alert (n); // error
Ii. Javascript variable scope chain
Copy codeThe Code is as follows:
Var x = '000 ';
Document. writeln (x); // get '000'
A ();
Function (){
Var x = 'aaa ';
Function B (){
Document. writeln (x); // undefined
Var x = 'bbb ';
Document. writeln (x); // bbb
}
B ();
Document. writeln (x); // aaa
}
// The result is: 000 undefined bbb aaa
Principle:
When using a variable, find it from the function block (called object in the authoritative guide,
If not, find the function block at the upper level until it is found,
If no definition is found until the top-level code (var x = '000';), the code reports an undefined error.
1. Output x '000' in the order of sequential execution (this is OK );
2. Then execute ()
3. Execute B () in ()
4. In B (), x needs to be output. This function has the definition of x in the body (scope), but it has not been assigned a value. Therefore, the output is undefined; (Focus !)
5. Output x and x are assigned values. Therefore, the bbb is output;
6. Output aaa at last;
After understanding the above principles, let's look at the example below
Copy codeThe Code is as follows:
Var x = "global ";
Function f (){
Var x = 'f1 ';
Function f2 (){
X = 'F2'; // here I am confused. GLOBAL X should be assigned to 'F2' again'
Alert (x); // returns "f2"
Alert (window. x); // return "global"
}
F2 ();
Alert (x) // returns "f2"
}
F ();
Alert (x); // returns "global", which is not reassigned to: f2
// The results are displayed respectively: f2 global f2 global
Explanation:
First, execute f2 () in f (),
F2 () generates a scope for the internal function. Therefore, x = 'F2' modifies the x value in f () rather than the global x.
Alert (x); is 'F2', alert (window. x) is 'global '.
Then execute alert (x); the scope of this x is global, Which is 'global'
3. Suggestions for new users
1. Reduce global variables (solution: encapsulate variables in objects)
Reference:
"Putting all the messy footprints you step on the whole under one person can significantly reduce the possibility of conflicts with other applications, gadgets, or JS libraries ."
-Douglas Crockford
Copy codeThe Code is as follows:
Var name = 'Jeffrey ';
Var lastName = 'way ';
Function doSomething (){...}
Console. log (name); // Jeffrey -- or window. name
Better Writing
Copy codeThe Code is as follows:
Var DudeNameSpace = {
Name: 'Jeffrey ',
LastName: 'way ',
DoSomething: function (){...}
}
Console. log (DudeNameSpace. name); // Jeffrey
Note: how can we dramatically classify "messy footprints" under "DudeNameSpace;
2. long column variable declaration? Do not write so many var values. Use a comma.
Copy codeThe Code is as follows:
Var someItem = 'some string ';
Var anotherItem = 'another string ';
Var oneMoreItem = 'One more string ';
Better Writing
Copy codeThe Code is as follows:
Var someItem = 'some string ',
AnotherItem = 'another string ',
OneMoreItem = 'One more string ';
It is self-evident. I don't know if this can improve the code execution speed, but it does make your code much cleaner.