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

Fix coding style in docs/iterator.md

This commit is contained in:
Xcat Liu 2016-05-18 14:49:05 +08:00
parent 307932e155
commit 3c84554957

View File

@ -29,15 +29,15 @@ it.next() // { value: "a", done: false }
it.next() // { value: "b", done: false }
it.next() // { value: undefined, done: true }
function makeIterator(array){
function makeIterator(array) {
var nextIndex = 0;
return {
next: function(){
next: function() {
return nextIndex < array.length ?
{value: array[nextIndex++], done: false} :
{value: undefined, done: true};
}
}
};
}
```
@ -52,15 +52,15 @@ function makeIterator(array){
对于遍历器对象来说,`done: false``value: undefined`属性都是可以省略的,因此上面的`makeIterator`函数可以简写成下面的形式。
```javascript
function makeIterator(array){
function makeIterator(array) {
var nextIndex = 0;
return {
next: function(){
next: function() {
return nextIndex < array.length ?
{value: array[nextIndex++]} :
{done: true};
}
}
};
}
```
@ -74,14 +74,14 @@ it.next().value // '1'
it.next().value // '2'
// ...
function idMaker(){
function idMaker() {
var index = 0;
return {
next: function(){
next: function() {
return {value: index++, done: false};
}
}
};
}
```
@ -166,31 +166,31 @@ for (var value of range(0, 3)) {
下面是通过遍历器实现指针结构的例子。
```javascript
function Obj(value){
function Obj(value) {
this.value = value;
this.next = null;
}
Obj.prototype[Symbol.iterator] = function(){
Obj.prototype[Symbol.iterator] = function() {
var iterator = {
next: next
};
var current = this;
function next(){
if (current){
function next() {
if (current) {
var value = current.value;
var done = current.next === null;
current = current.next;
return {
done: done,
value: value
}
};
} else {
return {
done: true
}
};
}
}
return iterator;
@ -204,7 +204,7 @@ one.next = two;
two.next = three;
for (var i of one){
console.log(i)
console.log(i);
}
// 1
// 2