JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

5.5.4 for/in

5.5.4 for/in


The for/instatement uses the forkeyword, but it is a completely different kind of loop than the regular for loop. A for/in loop looks like this: for (variable in object) statement

variable typically names a variable, but it may be any expression that evaluates to an lvalue ( §4.7.3 ) or a var statement that declares a single variable—it must be something suitable as the left side of an assignment expression. object is an expression that evaluates to an object. As usual, statement is the statement or statement block that serves as the body of the loop.

It is easy to use a regular for loop to iterate through the elements of an array: for(var i = 0; i < a.length; i++) // Assign array indexes to variable i console.log(a[i]); // Print the value of each array element

The for/in loop makes it easy to do the same for the properties of an object:

for(var p in o) // Assign property names of o to variable p console.log(o[p]); // Print the value of each property

To execute a for/in statement, the JavaScript interpreter first evaluates the object expression. If it evaluates to null or undefined, the interpreter skips the loop and moves on to the next statement.3 If the expression evaluates to a primitive value, that value is converted to its equivalent wrapper object ( §3.6 ). Otherwise, the expression is already an object. The interpreter now executes the body of the loop once for each enumerable property of the object. Before each iteration, however, the interpreter evaluates the variable expression and assigns the name of the property (a string value) to it.

Note that the variable in the for/in loop may be an arbitrary expression, as long as it evaluates to something suitable for the left side of an assignment. This expression is evaluated each time through the loop, which means that it may evaluate differently each time. For example, you can use code like the following to copy the names of all object properties into an array:

var o = {x:1, y:2, z:3};

var a = [], i = 0;

for(a[i++] in o) /* empty */;

JavaScript arrays are simply a specialized kind of object and array indexes are object properties that can be enumerated with a for/inloop. For example, following the code above with this line enumerates the array indexes 0, 1, and 2:

for(i in a) console.log(i);

The for/in loop does not actually enumerate all properties of an object, only the enumerable properties (see §6.7 ). The various built-in methods defined by core JavaScript are not enumerable. All objects have a toString() method, for example, but the

3. ECMAScript 3 implementations may instead throw a TypeError in this case.

for/inloop does not enumerate this toStringproperty. In addition to built-in methods, many other properties of the built-in objects are nonenumerable. All properties and methods defined by your code are enumerable, however. (But in ECMAScript 5, you can make them nonenumerable using techniques explained in §6.7 .) User-defined inherited properties (see §6.2.2 ) are also enumerated by the for/in loop.

If the body of a for/in loop deletes a property that has not yet been enumerated, that property will not be enumerated. If the body of the loop defines new properties on the object, those properties will generally not be enumerated. (Some implementations may enumerate inherited properties that are added after the loop begins, however.)

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27827.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1