For/in cycle of JavaScript loops

Source: Internet
Author: User
Tags javascript array

Today, I learned a JavaScript statement. As with other common programming languages such as C, Java, and so on, the statements in JavaScript contain: ① expression statements ② compound statements and empty statements ③ declaration Statements ④ Conditional Statements ⑤ loop Statements ⑥ jump statements, and of course JavaScript has ⑦ other three statements. So the title is called the For/in loop of the JavaScript loop, mainly because I think most of the statements in this is the same as C, Java, there is no need to waste their breath introduced, but the For/in loop is the first time in JavaScript contact. Of course, these are very basic content, just as a study notes, beginners JavaScript can see, mastered the need to waste time to read.

First of all, the first is a little bit more specific empty statement it. An empty statement has only one semicolon, which is:; , it is very simple, it makes you want to have fur use? Once heard that: if anything in the world exists, there must be a reason for its existence. An empty statement is no exception, it still has a bit of effect. When the JavaScript interpreter executes an empty statement, it obviously does not perform any action. However, the practice proves that empty statements are sometimes useful when creating a loop with an empty loop body. For example:

// Initializes an array of a  for (var i=0; i<a.length; a[i++]=0);

In this loop, all operations are done in the expression a[i++]=0 and do not require any loop body, but JavaScript needs to include at least one statement in the loop body, so there is an empty statement here. Of course, this can also be implemented with a while loop, but this simple point only.

The empty statement visually also acts on that point. As for the statement in JavaScript, it must be described in detail the variable declaration advance characteristics Ah, this is a JavaScript focus Ah, I'm sorry here is not said, if you want to know, please click: http://www.cnblogs.com/ Craftsman-gao/p/4720635.html. Conditional statements, if and else if are also the same as C, Java, there is no more nonsense, switch statement there is an easy to ignore the knowledge point. When the switch statement is executed, the value of the expression in parentheses after the switch is evaluated first, and then the value of the expression in the case clause is found to match the value of the expression in parentheses followed by the switch, with the emphasis being to compare whether the values of two expressions are used in conjunction with exactly equal, i.e. "= = = ".

Well, now to the real loop statement to say. There are four types of loops in javascript: while, Do/while, for, and For/in. The first three kinds are also not what characteristics, so directly said For/in. First look at its syntax:

1  for inch object) 2     Statement

Variable is usually a variable name, an expression that can produce an lvalue, or a variable declared through a var statement, which must be a value appropriate to the left of the assignment expression. object is an expression in which the expression evaluates to an object. Similarly, statement is a statement or statement block that forms the body of a loop. Using the for/in loop makes it easy to traverse object property members:

1  for (var in o) 2     Console.log (O[p]);    

During the execution of the For/in statement, the JavaScript interpreter first evaluates an object expression. If the expression is null or undefined, the JavaScript interpreter skips the loop and executes the subsequent code. If the expression equals an original value, the original value will be converted to the corresponding wrapper object. Otherwise, the expression itself is already an object. JavaScript enumerates the properties of the object in turn to perform the loop. Before each loop, JavaScript assigns an object's property name (a string) to variable.

It is important to note that as long as the value of variable in the for/in loop can be an lvalue of an assignment expression, it can be an arbitrary expression. For example, you can use the following code to copy the object properties used in an array:

1 var o = {x:1, y:2, Z:3}; 2 var a =[], i=0; 3  for  in O); // Note that the empty statement is used here.

Because the JavaScript array is a special object, the For/in loop can enumerate the array indexes as if they were enumerated object properties. For example:

1  for (var in arr) 2     Console.log (i); // the index value of the output array sequentially

Note: The for/in loop does not traverse all of the properties of an object, only the enumerable (enumerable) property will traverse (this is the object's content, and then do the details later). Built-in methods defined by the JavaScript language core are not "enumerable". For example, all objects have the method ToString (), but the for/in loop does not enumerate the ToString property. In addition to the built-in methods, many of the properties of built-in objects are also "non-enumerable." All of the properties and methods defined in the code are enumerable. Objects can inherit the properties of other objects, and those inherited custom properties can also be enumerated using for/in. With regard to the order of attribute enumeration, the ECMAScript specification is not specified, and the JavaScript implementations of the mainstream browser vendors enumerate the properties of simple objects in the order in which they are defined, first enumerating the attributes first defined. If you create an object in the form of an object's direct amount, it is enumerated in the order in which the attributes appear in the direct amount.

There are two points to note: A. If the for/in loop body removes an attribute that has not yet been enumerated, then this property will no longer be enumerated; b. If the loop body defines a new property for the object, these properties are not usually enumerated;

The other three kinds of statements are: ①with②debugger (for program break points when debugging a program) ③ "use strict". But let's not say three of them.

For/in cycle of JavaScript loops

Related Article

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.