【发布时间】:2015-08-07 06:57:54
【问题描述】:
我正在运行 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)
>
为什么会这样?
【问题讨论】:
-
不带“*”试试?
-
检查其他帖子是否有帮助 stackoverflow.com/questions/28388885/…
-
就像 tehnuty 说的,删除星号。也许你是一个 C/C++ 编码员——就像我一样——只是需要更多的咖啡。
-
asterisk is required 定义一个生成器函数。语法是新的,并不总是受支持。
标签: javascript node.js generator