Do not let go of any one detail. It is believed that many JavaScript developers hesitate to declare the loop variable where var I should be placed: how does it affect the operation of the program in a different location? Which approach conforms to JavaScript's language specification? Which approach matches the future development direction of the ECMA standard? This paper makes a simple analysis and comparison of the writing methods of four kinds of common declaration loop variables.
Habit 1: Do not declare direct use
function Loop (arr) { for (i = 0; i < arr.length; i++) { // do something
> }}
Very dangerous use of the habit, in general, the loop variable will become a Window object on a property is used globally, it is very likely to affect the normal logic implementation of the program, think of all the egg pain, we all understand, it is not here to repeat.
It should be emphasized that in the strict mode, the use of a direct assignment without declaring a variable will throw an exception directly, and that's long overdue! Quote a paragraph from Appendix C of the ECMA-262 standard:
"Assignment to an undeclared identifier or otherwise unresolvable reference does not create a property in the global objec T. When a simple assignment occurs within strict mode code, it lefthandside must not evaluate to an unresolvable Referenc E. If it does a Referenceerror exception is thrown (6.2.3.2). "
In other words, if you use an undeclared variable again, the Referenceerror exception is thrown.
Habit 2: Put in the For Loop initial statement block and declare repeatedly
function Loop (arr) { for (var i = 0; i < arr.length; I++ // do someting } // Console.log (i); for (var i = 0; i < arr.length; I++) {
//
do something else }}
This approach seems to be the safest specification, and many of the students who go from C and Java to the front end prefer this, in fact, perhaps because of a misunderstanding of a key concept in JavaScript-the variable scope. Unlike C and Java,javascript, there is no real block-level scope, which means that console.log (i) does not print undefined or throw referenceerror exceptions after the first loop is finished. Instead, the arr.length is printed normally.
Of course, this kind of writing is not significant except for aesthetics, but it has long been well-compatible and has not violated any specification the--ECMA standard does not prohibit duplicate declarations of the same variable within a scope. Not only that, but there is also another good news, in ECMAScript 6, a new, to support the real block-level scope of the keyword has emerged--let. Here is a portal, interested students can self-understanding:
Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
Habit 3: Centrally defined at the top of the function and with other variables
function Loop (arr) { var var1; var var2; var i; for (i = 0; i < arr.length; i++) { // do something }}
This c89-like-style variable definition is almost impeccable in JavaScript and does not cause JavaScript to support block-level scope misunderstandings, without contaminating the global scope, without violating any standards and specifications. The main drawback is that the declaration of the loop variable and the loop body may be separated far away. Without more code, this problem seems to be unable to find a better solution than waiting for major mainstream browser vendors to implement the LET keyword in ECMAScript 6.
Habit 4: Encapsulate the Loop code in Iife
function Loop (arr) { (function () { for (var i = 0; i < arr.length; I++
>) {
// do
something
} }) ();}
The last Habit is the Iife (immediately-invoked function Expression), which is familiar to front-end programmers, that is, executing functions immediately. The main disadvantage of this method is that it is relatively cumbersome to write and has excess performance loss (very small), but it performs well in compatibility and compliance with standard specifications. If it's not too much trouble, developers can take it this way.
The above is a simple introduction and analysis of the four common cyclic variables in JavaScript writing habits, each has pros and cons, readers can combine their own needs to use the preferred. It should be said that there is no perfect solution for defining cyclic variables before ECMAScript 6. Fortunately, the ECMAScript Standard Committee also found this problem in time, let's look forward to letting the keyword.
(End of full text)
Where should I put the circular variable declaration in JavaScript?