use*_*531 59 javascript ecmascript-harmony ecmascript-6
我将如何以与JS1.7 SomeClass.prototype.__iterator__ = function() {...}
语法相同的方式从ES6类中创建迭代器?
[编辑16:00]
以下作品:
class SomeClass {
constructor() {
}
*[Symbol.iterator]() {
yield '1';
yield '2';
}
//*generator() {
//}
}
an_instance = new SomeClass();
for (let v of an_instance) {
console.log(v);
}
Run Code Online (Sandbox Code Playgroud)
WebStorm标记*[Symbol.iterator]()
在asterix后面直接显示"函数名称预期"警告,但是否则会编译并与Traceur一起运行.(注意WebStorm不会生成任何错误*generator()
.)
ale*_*ods 35
您需要指定Symbol.iterator
为其SomeClass
返回类实例的迭代器的属性.迭代器必须有next()
方法,女巫反过来返回对象done
和value
字段.简化示例:
function SomeClass() {
this._data = [1,2,3,4];
}
SomeClass.prototype[Symbol.iterator] = function() {
var index = 0;
var data = this._data;
return {
next: function() {
return { value: data[++index], done: !(index in data) }
}
};
};
Run Code Online (Sandbox Code Playgroud)
或者使用ES6类和箭头函数:
class SomeClass {
constructor() {
this._data = [1,2,3,4];
}
[Symbol.iterator]() {
var index = -1;
var data = this._data;
return {
next: () => ({ value: data[++index], done: !(index in data) })
};
};
}
Run Code Online (Sandbox Code Playgroud)
用法:
var obj = new SomeClass();
for (var i of obj) { console.log(i) }
Run Code Online (Sandbox Code Playgroud)
在您更新的问题中,您通过生成器函数实现了类迭代器.你可以这样做,但你必须明白迭代器可能不是一个生成器.实际上es6中的迭代器是具有特定方法的任何对象next()
And*_*erg 35
定义合适的迭代器方法.例如:
class C {
constructor() { this.a = [] }
add(x) { this.a.push(x) }
[Symbol.iterator]() { return this.a.values() }
}
Run Code Online (Sandbox Code Playgroud)
编辑:示例使用:
let c = new C
c.add(1); c.add(2)
for (let i of c) console.log(i)
Run Code Online (Sandbox Code Playgroud)
Sha*_*ssy 21
这是在ES6中迭代2d矩阵自定义类的示例
class Matrix {
constructor() {
this.matrix = [[1, 2, 9],
[5, 3, 8],
[4, 6, 7]];
}
*[Symbol.iterator]() {
for (let row of this.matrix) {
for (let cell of row) {
yield cell;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这类课程的用法是
let matrix = new Matrix();
for (let cell of matrix) {
console.log(cell)
}
Run Code Online (Sandbox Code Playgroud)
哪个会输出
1
2
9
5
3
8
4
6
7
Run Code Online (Sandbox Code Playgroud)
eki*_*aby 11
文档:迭代协议
实现迭代器协议和可迭代协议技术的示例类:
class MyCollection {
constructor(elements) {
if (!Array.isArray(elements))
throw new Error('Parameter to constructor must be array');
this.elements = elements;
}
// Implement "iterator protocol"
*iterator() {
for (let key in this.elements) {
var value = this.elements[key];
yield value;
}
}
// Implement "iterable protocol"
[Symbol.iterator]() {
return this.iterator();
}
}
Run Code Online (Sandbox Code Playgroud)
使用任一技术访问元素:
var myCollection = new MyCollection(['foo', 'bar', 'bah', 'bat']);
// Access elements of the collection using iterable
for (let element of myCollection)
console.log('element via "iterable": ' + element);
// Access elements of the collection using iterator
var iterator = myCollection.iterator();
while (element = iterator.next().value)
console.log('element via "iterator": ' + element);
Run Code Online (Sandbox Code Playgroud)
使对象可迭代意味着该对象有一个以Symbol.iterator
. 当这个方法被调用时,它应该返回一个名为iterator的接口。
这个迭代器必须有一个next
返回下一个结果的方法。这个结果应该是一个具有value
提供下一个值的属性的对象,以及一个done
属性,true
当没有更多结果时应该是这样,false
否则。
我还将为一个名为的类实现一个迭代器,该类的Matrix
所有元素的范围都从0
到width * height - 1
。我将为这个迭代器创建一个不同的类,称为MatrixIterator
.
class Matrix {
constructor(width, height) {
this.width = width;
this.height = height;
this.content = [];
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
this.content[y * width + x] = y * width + x;
}
}
}
get(x, y) {
return this.content[y * this.width + x];
}
[Symbol.iterator]() {
return new MatrixIterator(this);
}
}
class MatrixIterator {
constructor(matrix) {
this.x = 0;
this.y = 0;
this.matrix = matrix;
}
next() {
if (this.y == this.matrix.height) return {done: true};
let value = {
x: this.x,
y: this.y,
value: this.matrix.get(this.x, this.y)
};
this.x++;
if (this.x == this.matrix.width) {
this.x = 0;
this.y++;
}
return {value, done: false};
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,通过定义符号来Matrix
实现迭代器协议Symbol.iterator
。在这个方法内部,MatrixIterator
创建了一个实例,它以为参数,this
即Matrix
实例作为参数,在内部 MatrixIterator
,next
定义了方法。我特别喜欢这种实现迭代器的方式,因为它清楚地展示了迭代器和Symbol.iterator
.
或者,也可以不直接定义Symbol.iterator
,而是添加一个函数prototype[Symbol.iterator]
如下:
Matrix.prototype[Symbol.iterator] = function() {
return new MatrixIterator(this);
};
Run Code Online (Sandbox Code Playgroud)
let matrix = new Matrix(3, 2);
for (let e of matrix) {
console.log(e);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16842 次 |
最近记录: |