This article mainly introduces examples of declaring variables or functions in JavaScript in advance. This article describes a small technique or feature. For more information, see the following example, see the following example.
(You can use the Chrome browser, and then right-click F12/or review the elements. Call out the developer tool and enter the console)
(TIPS: Shift + Enter in the console can be used to wrap code in the middle)
The Code is as follows:
Var name = "xiaoming ";
(Function (){
Var name = name | "Xiao Zhang ";
Console.info (name );
}) (); // Xiaozhang
(Function (){
Name = name | "Xiao Zhang ";
Console.info (name );
}) (); // Xiaoming
(Function (){
Var name2 = name;
Var name = name | "Xiao Zhang ";
Console.info (name, name2 );
}) (); // Small piece of undefined
The execution is as follows:
Explanation:
In JavaScript.
The Code is as follows:
Function xxx (){
// A bunch of code...
//...
Var name2 = name;
Var name = name | "Xiao Zhang ";
// A bunch of code
}
The execution is equivalent to the following:
The Code is as follows:
Function xxx (){
Var name2 = undefined;
Var name = undefined;
// Other var values are also pushed to the starting position.
// A bunch of code...
//...
Name2 = name;
Name = name | "Xiao Zhang ";
// A bunch of code
}