mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-25 03:02:21 +00:00
add docs/iterator
This commit is contained in:
parent
1c7b2966a3
commit
7fc3348d84
51
docs/iterator.md
Normal file
51
docs/iterator.md
Normal file
@ -0,0 +1,51 @@
|
||||
# Iterator:遍历器
|
||||
|
||||
遍历器(Iterator)是一种协议,任何对象只要部署这个协议,就可以完成遍历操作。在ES6中,遍历操作特指for...of循环。
|
||||
|
||||
ES6的遍历器协议规定,部署了next方法的对象,就具备了遍历器功能。next方法必须返回一个包含value和done两个属性的对象。其中,value属性是当前遍历位置的值,done属性是一个布尔值,表示遍历是否结束。
|
||||
|
||||
```javascript
|
||||
|
||||
function makeIterator(array){
|
||||
var nextIndex = 0;
|
||||
|
||||
return {
|
||||
next: function(){
|
||||
return nextIndex < array.length ?
|
||||
{value: array[nextIndex++], done: false} :
|
||||
{value: undefined, done: true};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var it = makeIterator(['a', 'b']);
|
||||
|
||||
it.next().value // 'a'
|
||||
it.next().value // 'b'
|
||||
it.next().done // true
|
||||
|
||||
```
|
||||
上面代码定义了一个makeIterator函数,它的作用是返回一个遍历器对象,用来遍历参数数组。请特别注意,next返回值的构造。
|
||||
|
||||
下面是一个无限运行的遍历器例子。
|
||||
|
||||
```javascript
|
||||
|
||||
function idMaker(){
|
||||
var index = 0;
|
||||
|
||||
return {
|
||||
next: function(){
|
||||
return {value: index++, done: false};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var it = idMaker();
|
||||
|
||||
it.next().value // '0'
|
||||
it.next().value // '1'
|
||||
it.next().value // '2'
|
||||
// ...
|
||||
|
||||
```
|
@ -5,8 +5,9 @@
|
||||
授权:<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">署名-非商用许可证</a>
|
||||
|
||||
## 目录
|
||||
0. [README](#README)
|
||||
1. [ECMAScript 6简介](#docs/intro)
|
||||
- [首页:Readme](#README)
|
||||
- [ECMAScript 6简介](#docs/intro)
|
||||
- [Iterator:遍历器](#docs/iterator)
|
||||
|
||||
## 其他
|
||||
- [源码](http://github.com/ruanyf/es6tutorial/)
|
||||
|
Loading…
x
Reference in New Issue
Block a user