I. Variables
• Global Variables
The two features of JavaScript make it unexpected to unconsciously create global variables. First, you can use variables without even declaring them. Second, JavaScript has an implicit global concept, this means that any variable you do not declare will become a global object attribute (not a global variable in the true sense, you can delete it using delete)
Copy codeThe Code is as follows:
Function sum (x, y ){
// The result is not declared. It is an implicit global variable.
Result = x + y;
Return result;
}
Function foo (){
// Use the task chain for partial var declaration. B is an implicit global variable.
Var a = B = 1;
}
Suggestion:
Copy codeThe Code is as follows:
Function (x, y ){
Var a, B;
A = B = 1; // a, B is the local variable
}
• Var Function
Global variables created through var (created in programs other than functions) cannot be deleted. Implicit global variables created without var (ignore whether or not they are created in the function) can be deleted.
Copy codeThe Code is as follows:
// Define three global variables
Var global_var = 1;
Global_novar = 2; // textbook
(Function (){
Global_fromfunc = 3; // negative textbook
}());
// Try to delete
Delete global_var; // false
Delete global_novar; // true
Delete global_fromfunc; // true
// Test the deletion.
Typeof global_var; // "number"
Typeof global_novar; // "undefined"
Typeof global_fromfunc; // "undefined"
• Declare variables in the form of single var
Using a single var statement at the top of a function is a useful form. The initial values of all uninitialized but declared variables are undefined.
Copy codeThe Code is as follows:
Function func (){
Var a = 1,
B = 2,
Sum = a + B,
Myobject = {},
I,
J;
// Function body...
}
• Var Distribution
Copy codeThe Code is as follows:
// Counterexample
Myname = "global"; // global variable
Function func (){
Alert (myname); // "undefined" var myname = "local"; alert (myname); // "local"} func (); equivalent:
Myname = "global"; // global variable
Function func (){
Var myname; // equivalent to-> var myname = undefined;
Alert (myname); // "undefined"
Myname = "local ";
Alert (myname); // "local "}
Func ();
2. for Loop
• Recommended
Copy codeThe Code is as follows:
Function logoff (){
Var I = 0,
Max,
Myarray = [];
//...
For (I = 0, max = myarray. length; I <max; I ++ ){
// Use myarray [I] to do something
}
}
Use the following expression to replace I ++
Copy codeThe Code is as follows:
I = I + 1
I + = 1 the following two cycles are faster
// The first form of change:
Var I, myarray = [];
For (I = myarray. length; I --;){
// Use myarray [I] to do something
}
// The second method uses the while loop:
Var myarray = [],
I = myarray. length;
While (I --){
// Use myarray [I] to do something
}
• For-in Loop
Apply to non-array object traversal. The array uses a normal for loop, and the object uses a for-in loop. The hasOwnProperty () method filters out attributes from the prototype chain when traversing object attributes.
Iii. Avoid implicit type conversion
• Always use = and! =
4. Avoid using eval, and avoid passing strings to setInterval (), setTimeout (), and Function () constructor, instead of using functions.
Five parseInt () numeric Conversion
We recommend that you assign a value to the base parameter,
Copy codeThe Code is as follows:
Var month = "06 ",
Year = "09 ";
Month = parseInt (month, 10); // a string starting with 0 is treated as an octal string.
Year = parseInt (year, 10 );
Six programming specifications
Name of the constructor: MyConstructor ();
General function name: myFunction ();
Variable name: firstName;
Private property or method: _ secondeName,
Constant: PI, MAX;