Analysis of four kinds of common JavaScript declaration loop variable Writing mode __java

Source: Internet
Author: User
 A brief analysis of the writing method of four kinds of more common JavaScript declaration loop variables

This article mainly first contained four kinds of declarations of the circular variable of writing, about its holding a complex analysis and a bit, the necessary users can refer to the
The circular variable declaration in JavaScript is where it should be.

habit 1: Do not declare indirect use

function Loop (arr) {for 
 (i = 0; i < arr.length. i++) { 
  //do something 
 } 

is very thrilling to use the wind is not elegant, the individual environment of the reincarnation variable will become a window on the image of a property is applied globally, there is a very approximate impact order of the individual logic implementation.
It is important to mention that, in the strict situation, the use of indirect assignment without declaring variables will be indirectly thrown out of the normal, long overdue. Invoke the passage in Appendix C of the ECMA-262 standard:
"Assignment to" 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 a unresolvable E. If it does a Referenceerror exception is thrown (6.2.3.2). "
In other words, if you use undeclared variables again, Referenceerror will normally be thrown.


Habit 2: Put in for Samsara the initial statement block and frequently declare

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 type of approach appears to be the most secure specification, and many students who go from C and Java to front-end development tend to prefer this, in fact, because of the variable scope, which is caused by a misunderstanding of an important concept in JavaScript. The difference between C and Java,javascript actually does not have a real block-level functional domain, it is said, is the first cycle of stagnation, console.log (i) In fact will not print undefined about throw referenceerror Normal, Instead, it will print out the arr.length individually.
It is true that the way in which it is written is not insignificant except for the ugly, but it has long been compatible and has not defied any specification--ECMA the standard does not suppress repeated declarations of opposing variables in a functional field.


Habit 3: Merging concepts with other variables at the top of a function

function Loop (arr) { 
 var var1; 
 var var2; 
 var i; 
 
 for (i = 0; i < arr.length. i++) { 
  //do something 
 } 

This type of c89-like variable concept is somewhat optional in JavaScript and does not cause JavaScript to support block-level functional domain misunderstandings without contaminating global scope and defying any standards and specifications. The main drawback is that the declaration of the cyclic variable and the body of the reincarnation will be a little far away. is not the use of more code, in addition to waiting for the major mainstream scanner manufacturers realize ECMAScript 6 in the Let-key word, this title seems to find no better plan to fix.


Habit 4: Encapsulate looping code into Iife

function Loop (arr) { 
 (function () {for 
  (var i = 0; i < arr.length; i++) { 
   //do Something 
  } 
 }) (); 

The last habit is the familiar iife (I sister ediately-invoked function Expression) of the front-end order, which immediately implements the function. The main disadvantage of this approach is that the transcription of trouble, and the excess of functional consumption (very small), but is in compatibility, on the compliance of the standard specifications to indicate excellence. If it's not too much trouble, the Blazers can use this kind of approach.

The above is about four kinds of javascript more see the concept of reincarnation variable the complexity of the first volume and analysis, there are no pros and cons, readers can be their own needs of the preferred use.


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.