I. about JavaScript variable declarations
In JavaScript, declare a variable
var a=1;
can also be directly
A=1;
There is a difference between the two expressions,
One is the local variable of the current scope and the other is the global variable of the current scope;
The special point of the JavaScript language is that the global variable can be read directly from within the function.
Copy Code code as follows:
var n=999;
Function F1 () {
alert (n);
}
F1 (); 999
On the other hand, local variables within a function cannot naturally be read outside the function.
Copy Code code as follows:
Function F1 () {
var n=999;
}
alert (n); Error
Two. JavaScript variable scope chain
Copy Code code as follows:
var x= ' 000 ';
Document.writeln (x); Draw ' 000 '
A ();
function A () {
var x= ' AAA ';
Function B () {
Document.writeln (x); Undefined
var x= ' BBB ';
Document.writeln (x); Bbb
}
b ();
Document.writeln (x); Aaa
}
The result: undefined BBB AAA
Principle:
When a variable is used, it is found in the function block, which is explained by the calling object in the authoritative guide.
If you can't find it, find it from the upper-level function block until you find it.
If the definition has not been found until the top level code (the position of Var x= ' 000) has been identified, the code will report an undefined error.
1. Sequentially executed order, output X ' 000 ' (this is OK);
2. Then execute a ()
3. Implementation of B () in A ()
Output x in 4.b (), which has an X definition in the body (scope), but is not yet assigned, so outputs undefined; )
5. Then output x,x has been assigned value, so output BBB;
6. The final output of AAA;
Understanding the above principles, let's look at the following example
Copy Code code as follows:
var x = "global";
function f () {
var x= ' F1 ';
function F2 () {
x= ' F2 '//I'm a little confused here, and the global X should be assigned to ' F2 ' again.
alert (x); Return to "F2"
alert (window.x); Return to "global"
}
F2 ();
Alert (x)//Return to "F2"
}
f ();
alert (x); Returns "Global" and has not been assigned a value of: F2
Results are ejected separately: F2 global F2 Global
Explain:
First execute the F2 () in F (),
F2 () produces a scope for the intrinsic function, so x= ' F2 ' modifies the X value in F (), not the global X.
alert (x); is ' F2 ', alert (window.x) is ' global '.
And then execute alert (x), where the scope of this x is global and the ' global '
three. Suggestions for beginners
1. Reduce Global variables (solution: Encapsulate variables into objects)
Reference:
"Put your foot in the overall situation of those messy footprints are attributed to one person, can significantly reduce the use of other applications, gadgets or JS library conflict possibilities." ”
–douglas Crockford
Copy Code code as follows:
var name = ' Jeffrey ';
var lastName = ' Way ';
function dosomething () {...}
Console.log (name); Jeffrey--or Window.name
A better way to spell
Copy Code code as follows:
var dudenamespace = {
Name: ' Jeffrey ',
LastName: ' Way ',
Dosomething:function () {...}
}
Console.log (Dudenamespace.name); Jeffrey
Notice how we dramatically put "messy footprints" under the object of "Dudenamespace";
2. A long list of variable declarations? Don't write so many Var, with commas.
Copy Code code as follows:
var Someitem = ' some string ';
var Anotheritem = ' another string ';
var onemoreitem = ' One more string ';
A better way to spell
Copy Code code as follows:
var Someitem = ' Some string ',
Anotheritem = ' Another string ',
Onemoreitem = ' one more string ';
It is self-evident. I don't know if this will improve the speed of code execution, but it does make your code a lot cleaner.