Learning TypeScript 2.x
上QQ阅读APP看书,第一时间看更新

Iterate values in an iterable (for...of)

In JavaScript, some built-in types are built-in iterables with a default iteration behavior. To be an iterable, an object must implement the @@iterator method, meaning that the object (or one of the objects in its prototype chain) must have a property with a @@iterator key, which is available via constant Symbol.iterator.

The for...of statement creates a loop iterating over iterable objects (including array, map, set, string, arguments object, and so on):

let iterable = [10, 20, 30]; 
 
for (let value of iterable) { 
  value += 1; 
  console.log(value); 
} 
You will learn more about iterables in  Chapter 4 , Object-Oriented Programming with TypeScript.