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