SyntaxError:意外的标识符(ES6中的生成器)

Ali*_*xel 12 javascript yield generator node.js ecmascript-harmony

在阅读了MDN生成器文档后,我想出了这个简单的实验:

var nodes = {
    type: 'root',
    value: [
        { type: 'char', value: 'a' },
        { type: 'char', value: 'b' },
        { type: 'char', value: 'c' },
    ],
};

function* recursiveGenerator(node) {
    if (node.type === 'root') {
        node.value.forEach(function (subnode) {
            for (var suffix of recursiveGenerator(subnode)) {
                yield suffix;
            }
        });
    }

    else {
        yield node.value;
    }
}

for (generated of recursiveGenerator(nodes)) {
    console.log(generated);
}
Run Code Online (Sandbox Code Playgroud)

在node.js v0.11.9上运行它并--harmony设置了标志会产生以下错误:

alix@900X4C:~$ node --version
v0.11.9
alix@900X4C:~$ node --harmony test.js 

/home/alix/test.js:14
                yield suffix;
                      ^^^^^^
SyntaxError: Unexpected identifier
Run Code Online (Sandbox Code Playgroud)

我也尝试使用for ... in ...let关键字代替var,但没有任何成功.

我不明白究竟yield*是什么,但如果我在for循环中使用它我会得到:

alix@900X4C:~$ node --harmony test.js 

/home/alix/test.js:14
                yield* suffix;
                ^
ReferenceError: yield is not defined
Run Code Online (Sandbox Code Playgroud)

如果我用for console.log()输出替换for中的yield a,bc.我究竟做错了什么?


编辑

这是一个简约的生成器,显示node.js知道如何处理生成器:

function* alpha() {
    yield 'a';
    yield 'b';
    yield 'c';
}

for (var suffix of alpha()) {
    console.log(suffix);
}
Run Code Online (Sandbox Code Playgroud)

输出:

alix@900X4C:~$ node --harmony y.js 
a
b
c
Run Code Online (Sandbox Code Playgroud)

解决方案(感谢@Andrew)

function* recursiveGenerator(node) {
    if (node.type === 'root') {
        for (var i = 0; i < node.value.length; ++i) {
            var subnode = node.value[i];

            for (var suffix of recursiveGenerator(subnode)) {
                yield suffix;
            }
        }
    }

    else {
        yield node.value;
    }
}

for (generated of recursiveGenerator(nodes)) {
    console.log(generated);
}
Run Code Online (Sandbox Code Playgroud)

vku*_*kin 23

总结注释:你不能使用yield一个内部定期的功能,所以你不能使用yieldforEach.这里有一个"发电机"foreach的例子:

function * foreach (arr, fn) {
  var i

  for (i = 0; i < arr.length; i++) {
    yield * fn(arr[i])
  }
}

function * gen (number) {
  yield number + 1
  yield number + 2
  yield number + 3
}

function * other () {
  yield * foreach([1, 2, 3], gen)
}

for (var i of other()) {
    console.log(i)
}
Run Code Online (Sandbox Code Playgroud)

更新 使用这样的助手也可以非常优雅地解决原始问题:

var nodes = {
  type: 'root',
  value: [
    { type: 'char', value: 'a' },
    { type: 'char', value: 'b' },
    { type: 'root', value: [
        { type: 'char', value: 'c' },
        { type: 'char', value: 'd' },
        { type: 'char', value: 'e' },
      ] 
    },
  ],
}

function * foreach (arr, fn) {
  var i

  for (i = 0; i < arr.length; i++) {
    yield * fn(arr[i])
  }
}

function * value (val) {
  yield val
}

function * recursiveGenerator(node) {
  yield * node.type === 'root' ?  foreach(node.value, recursiveGenerator) : value(node.value)
}

for (var generated of recursiveGenerator(nodes)) {
  console.log(generated);
}
Run Code Online (Sandbox Code Playgroud)

所以发电机本身就变成了单线!