SyntaxError:意外的标识符(ES6中的生成器) [英] SyntaxError: Unexpected Identifier (Generators in ES6)

查看:554
本文介绍了SyntaxError:意外的标识符(ES6中的生成器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读文档后,我想出了这个简单的实验。来自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);
}

使用 - 和谐标志设置产生以下错误:

Running it on node.js v0.11.9 with the --harmony flag set produces the following error:

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

/home/alix/test.js:14
                yield suffix;
                      ^^^^^^
SyntaxError: Unexpected identifier

我也尝试过使用 for ... in ... let 关键字而不是 var ,但没有任何成功。

I also tried using for ... in ... and the let keyword instead of var, but without any success.

我不明白 yield * 完全确实,但如果我在中使用它对于循环我改为:

I don't understand what yield* does exactly, but if I use it within the for loop I get instead:

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

/home/alix/test.js:14
                yield* suffix;
                ^
ReferenceError: yield is not defined

如果我替换收益率for console.log()它输出 a b c 。我做错了什么?

If I replace the yield in the for with console.log() it outputs a, b and c. What am I doing wrong?

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

Here's a minimalistic generator, showing that node.js knows what to do with generators:

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

for (var suffix of alpha()) {
    console.log(suffix);
}

输出:

alix@900X4C:~$ node --harmony y.js 
a
b
c






解决方案(感谢@Andrew)




Solution (thanks @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);
}


推荐答案

总结评论:你可以在常规函数中使用 yield ,因此您无法使用 yield 的forEach 。这里是生成的foreach的一个例子:

Summarizing the comments: you can't use yield inside a regular function, so you can't use yield with forEach. Here an example of "generatorized" 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)
}

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

UPDATE Also the original problem can be solved quite elegantly using such a helper:

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);
}

因此,发电机本身就变成一线了!

So the generator itself becomes a one-liner!

这篇关于SyntaxError:意外的标识符(ES6中的生成器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆