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

11.4.1 The for/each Loop

11.4.1 The for/each Loop


The for/eachloop is a new looping statement standardized by E4X. E4X (ECMAScript for XML) is a language extension that allows XML tags to appear literally in JavaScript programs and adds syntax and API for operating on XML data. E4X has not been widely implemented in web browsers, but it is supported by Mozilla’s JavaScript 1.6 (released in Firefox 1.5). In this section, we’ll cover only the for/each loop and its use with non-XML objects. See §11.7 for details on the rest of E4X.

The for each loop is much like the for/in loop. Instead of iterating through the properties of an object, however, it iterates through the values of those properties:

let o = {one: 1, two: 2, three: 3} for(let p in o) console.log(p); // for/in: prints 'one', 'two', 'three' for each (let v in o) console.log(v); // for/each: prints 1, 2, 3

When used with an array, the for/each loop iterates through the elements (rather than the indexes) of the loop. It typically enumerates them in numerical order, but this is not actually standardized or required:

a = ['one', 'two', 'three']; for(let p in a) console.log(p); // Prints array indexes 0, 1, 2 for each (let v in a) console.log(v); // Prints array elts 'one', 'two', 'three'

Note that the for/each loop does not limit itself to the array elements of an array—it will enumerate the value of any enumerable property of the array including enumerable methods inherited by the array. For this reason, the for/each loop is usually not recommended for use with arrays. This is particularly true for code that must interoperate with versions of JavaScript before ECMAScript 5 in which it is not possible to make user-defined properties and methods non-enumerable. (See §7.6 for a similar discussion of the for/in loop.)

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