我正在运行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)
>
为什么会这样?
答案 0 :(得分:10)
生成器函数当前在标志ngx_http_subrequest
后面的节点中可用。请参阅Features Implemented in V8 and Available in Node。
答案 1 :(得分:4)
带星号的函数采用ECMAScript 6语法。
function* name()
我认为你的nodejs无法识别它。启动服务器时使用--harmony
标志。