Writing maintainable code)
The maintained code means:
- Readable
- Consistent
- Predictable
- It looks like it was written by the same person.
- Recorded
Minimizing globals)
Each JavaScript environment has a global object that can be accessed when you use this outside any function. All the variables you create become the attributes of the global object. In the browser, for convenience, this global object has an additional attribute called window, which usually points to the global object itself.
Myglobal = "hello"; // not recommended
Console. Log (myglobal); // "hello"
Console. Log (window. myglobal); // "hello"
Console. Log (window ["myglobal"]); // "hello"
Console. Log (this. myglobal); // "hello"
Global Variables
The empirical rule is to always use VaR to declare variables;
Side effects when forgetting VaR)
There is a small difference between an implicit global variable and a clearly defined global variable.deleteOperator makes the variable undefined.
- 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.
This indicates that, technically, implicit global variables are not real global variables, but they are attributes of global objects. The attribute can bedeleteThe operator is deleted, but the variable cannot.
In es5 strict mode, an error is thrown when undeclared variables (such as the two opposite textbooks in the previous code snippet) work.
Access to the global object )【...]
In strict mode, you must take different forms. For example, if you are developing a javascript library, you can wrap your code in a real-time function, and then pass a reference pointing to this as a parameter of your real-time function from the global scope.
Single var Pattern)
Using a single var statement at the top of a function is a useful form;
Pre-resolution: the problem of VaR distribution (Hoisting: a problem with scattered vars)
In JavaScript, you can declare multiple var statements at any position of the function, and they serve as if they were declared at the top of the function, this behavior is called hoisting (mounting/top resolution/pre-resolution). Therefore, to avoid confusion, it is best to declare all the variables you want to use in advance.
For the sake of completeness, we will mention a little more complex at the execution layer. Code processing is divided into two stages. The first stage is variable, function declaration, and normal format parameter creation, which is a stage of parsing and entering the context. The second stage is code execution. function expressions and unqualified Identifiers (declared variables) are created. However, for practical purposes, we adopt the "hoisting" concept, which is not defined in the ecmascript standard and is usually used to describe behavior.
For Loop (for loops)
When you obtain values cyclically, the length of the cached array (or set) is better,
For (VAR I = 0, max = myarray. length; I <Max; I ++ ){
// Use myarray [I] to do something
}
Note that when you explicitly want to modify the set in the loop (for example, adding more DOM elements), you may prefer length update instead of constants.
With the single var form, you can extract variables from the loop, as shown below:
Function logoff (){
VaR I = 0,
Max,
Myarray = [];
//...
For (I = 0, max = myarray. length; I <Max; I ++ ){
// Use myarray [I] to do something
}
}
This form has the benefit of consistency because you stick to the single var form. The disadvantage is that it is difficult to copy and paste the entire loop when refactoring the code. For example, if you copy a loop from a function to another function, You have to determine whether you caniAndmaxIntroduce new functions (if they are not used here, you may need to delete them from the original functions ).
The last one to adjust the loop is to use one of the following expressions to replace
i++;
i = i + 1
i += 1
There are two other forms of change, which have been slightly improved because :【...]
- One Variable missing (no max)
- Counting down to 0 is usually faster, because comparing with 0 is more efficient than comparing with the array length or something other than 0.
<SCRIPT>
// The first form of change:
VaR I, myarray = ['A', 1, 'B', 2, 'C', 3];
For (I = myarray. length; I-= 1 ;){
// Use myarray [I] to do something
Console. Log (I );
}
// The second method uses the while loop:
VaR myarray = ['A', 1, 'B', 2, 'C', 3],
I = myarray. length;
While (I-= 1 ){
// Use myarray [I] to do something
Console. Log (I );
}
</SCRIPT>
---------------------------------------------------------------------- August 1, October 24, 2014 ------------------------------------------------------------------------------------------------------------------------------------------------------
Http://www.cnblogs.com/TomXu/archive/2011/12/28/2286877.html
For-in loop (for-in loops)
It is best to use a normal for loop for arrays and a for-in loop for objects.
There is a very importanthasOwnProperty()When traversing object attributes, you can filter out the attributes from the prototype chain.
Write high-quality code [Notes]