1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 18:32:22 +00:00

edit iterator/for...of

This commit is contained in:
ruanyf 2015-11-24 16:20:33 +08:00
parent b12393b3a9
commit 52ba0574f6

View File

@ -525,10 +525,10 @@ arr.forEach(function (element, index) {
});
```
JavaScript原有的`for...in`循环只能获得对象的键名不能直接获取键值。ES6提供for...of循环允许遍历获得键值。
JavaScript原有的`for...in`循环只能获得对象的键名不能直接获取键值。ES6提供`for...of`循环,允许遍历获得键值。
```javascript
var arr = ["a", "b", "c", "d"];
var arr = ['a', 'b', 'c', 'd'];
for (let a in arr) {
console.log(a); // 0 1 2 3
@ -541,6 +541,23 @@ for (let a of arr) {
上面代码表明,`for...in`循环读取键名,`for...of`循环读取键值。如果要通过`for...of`循环,获取数组的索引,可以借助数组实例的`entries`方法和`keys`方法,参见《数组的扩展》章节。
`for...of`循环调用遍历器接口,数组的遍历器接口只返回具有数字索引的属性。这一点跟`for...in`循环也不一样。
```javascript
let arr = [3, 5, 7];
arr.foo = 'hello';
for (let i in arr) {
console.log(i); // "0", "1", "2", "foo"
}
for (let i of arr) {
console.log(i); // "3", "5", "7"
}
```
上面代码中,`for...of`循环不会返回数组`arr``foo`属性。
### Set和Map结构
Set和Map结构也原生具有Iterator接口可以直接使用`for...of`循环。