我正在运行 NodeJS 控制台:
$ node --version
v0.12.0
我正在尝试实现一个生成器函数,例如这个
function* colorGen() {
var colors = ["red", "green", "blue", "white"]
var i = 0;
yield colors[i];
i += 1;
if (i > 3) {i = 0;}
}
但是当我运行第一行时,出现语法错误:
$ node
> function* colorGen() {
SyntaxError: Unexpected token *
at Object.exports.createScript (vm.js:44:10)
at REPLServer.defaultEval (repl.js:117:23)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
at REPLServer.Interface._onLine (readline.js:214:10)
at REPLServer.Interface._line (readline.js:553:8)
at REPLServer.Interface._ttyWrite (readline.js:830:14)
at ReadStream.onkeypress (readline.js:109:10)
>
为什么会发生这种情况?
生成器函数当前可在标记后面的节点中使用--harmony_generators
. See 在 V8 中实现并在 Node 中可用的功能 https://github.com/joyent/node/wiki/ES6-%28a.k.a.-Harmony%29-Features-Implemented-in-V8-and-Available-in-Node.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)