ITERATION AND SPREAD OPERATORS
ECMAScript 6 introduced iterators and the spread operator, which are especially useful in the context of collection reference types. These new tools allow for easy interoperability, cloning, and modification of collection types.
As shown earlier in the chapter, four native collection reference types define a default iterator:
Array
All typed arrays
Map
Set
Trivially, this means that all support ordered iteration and can be passed to a for..of
loop:
let iterableThings = [
Array.of(1, 2),
typedArr = Int16Array.of(3, 4),
new Map([[5, 6], [7, 8]]),
new Set([9, 10])
];
for (const iterableThing of iterableThings) {
for (const x of iterableThing) {
console.log(x);
}
}
// 1
// 2
// 3
// 4
// [5, 6]
// [7, 8]
// 9
// 10
This also means that all these types are compatible with the spread operator. The spread operator is especially...