I know, some people (such as before I) write JS for the For loop, are not used to add Var, which is of course allowed by the syntax. For example, below.
For (i=0; I<; I+ +) {// will not be written: Var i=0
alert (i);
}
However, this is really not a good habit, the following I would like to say why write JS for loop must add Var, otherwise it will occasionally bring you annoying difficult to check the bug.
For example, now we are going to implement this function: output
10
20
30
40
50
60
70
80
90
100
Through the following code implementation, Writenumber from 1 to 10 loops, each loop calls the Tentimes method to return 10 times times the index value.
1<Script type="Text/javascript">
2 functionWritenumber () {
3 For(I= 1; I<= 10; I++) {
4document.write (Tentimes (i)+ "<br/>")
5}
6}
7 functionTentimes (v) {
8 VarResult= 0;
9alert (i);
10 For(I= 1; I<= 10; I++) {
11Result+= V;
12
13 return result;
14
15 Writenumber ();
16 //alert (i)
17 </script>
You will find that only 10 of the output is finished. You can run the test with the following code box.
<textarea id="txtCode1" class="textareaJay"><script type= "Text/javascript" > Function Writenumber () {for (i = 1; i <=; i++) { document.write (Tentimes (i) + "<br/>"); }} function Tentimes (v) {var result = 0; alert (i); for (i = 1; i <=; i++) {result + = V; } return result; } writenumber (); Alert (i) </script></textarea>
About adding Var to the Writenumber and Tentimes methods, i.e. declaring the index variable i has 4 cases:
In the first case, Writenumber and Tentimes each have 1 for loops, and none of the 2 loops uses VAR to declare the i index variable.
Operation Result: Will alert out 1. The result is only 10 output, not what we want.
Analysis: When executing writenumber, the declared variable i is not found in its scope, and I is assigned directly to I, then I is implicitly declared as a global variable ( for a variable that is not declared inside a function, if it is assigned a value, it is implicitly declared as a global variable.) the loop starts, I=1, Tentimes method, found Tentimes method also did not declare the variable i, so tentimes i is the global variable I, and Writenumber I became the same. At this time line9 alert out of nature is 1. Tentimes Cycle 10 times, so that the global I into 11, natural Writenumber will not perform the 2nd cycle operation.
Verification: If you add alert (i) after the Writenumber () statement, or uncomment the LINE16, you will find the alert out of 12 (12=10+2 i++), which proves I is the Windows object at this time.
In the second case, Writenumber declares the I variable, that is, Line3:var I=1,tentimes does not declare I variable, namely Line10:i=1.
Operation Result: Line9 alert (i) I did not define the error because Writenumber has declared the variable i, so there is no global i,tentimes execution without declaring I, so the report is undefined. If you comment out Line9, the output is correct. Because when Tentimes runs to I=1, the implicit declaration of I is a global variable and does not affect the I in Writenumber. The Writenumber will still perform 10 cycles.
Verification: If you add alert (i) after the Writenumber () statement, or cancel the Line16 comment, you will find the alert out of one (11=10+tentimes i++), proving that there is windows.i at this time.
In the third case, Writenumber does not declare the I variable, i.e. line3:i=1,tentimes declares the i variable, i.e. Line10:var i=1.
Running result: 10 undefined pop up. Because Writenumber does not declare I, implicitly declares I as a global variable, and Tentimes has a declared over variable I (in addition, the declaration of a variable is done in precompilation), so line9 alert (i) I is not a windows.i, but a variable I declared by Tentimes, which is of course undefined. At the same time, it is found that the output is correct, because the tentimes I does not affect the Writenumber global i,writenumber still executes 10 cycles.
The fourth case: both Writenumber and tentimes declare I with VAR.
Running results: Comment out line9, do not say, good habits, the results of course perfect.
Although the second to third case output is correct, but the use of I is very confusing, it should be lucky to cause the result is correct, because just one is window.i, one is the function inside the private variable i, so that there is no conflict.
Although this article is about why the write for loop must add Var, it is actually about the scope of the variable (or the life cycle of the variable). After understanding, the following 2 sections of code run results you should be able to tell the exact answer.
<script type= "Text/javascript" > var a = 1; function f () {alert (a); var a = 2; } f (); </script>
<script type= "Text/javascript" > var a = 1; function f () {a = 2; alert (a); } f (); alert (a); </script>
Ps: Said coding's good habit, think of this: if (a==3) should be written if (3==a). Because we often write = = as 1 =, if the variable is written on the right side only 1 =, will be reported compile errors, so that the error can be found in time.
JS for loop, why must I add var to define I variables