语法错误:generator函数参数为unex

时间:2014-08-11 00:55:41

标签: javascript node.js

我对节点环境中生成器的语法感到困惑。

我将脚本运行为:node - harmony simpleGenerator.js,但仍有问题。我的节点环境是Linux(v0.11.5 / v0.11.13)

Syntax Error: Unexpected identifier

/home/NodeProj/ES6-generator/simpleGenerator.js:6
        yield x;
              ^

脚本:

function foo(x) {
    while (true) {
        x = x * 2;
        yield x;
    }
}

var gen = foo(1);

console.log(gen.next().value);

这里发生了什么?

1 个答案:

答案 0 :(得分:0)

生成器需要function关键字后面的星号:

function* foo(x) {
    while (true) {
        x = x * 2;
        yield x;
    }
}
相关问题