NodeJS控制台SyntaxError:生成器意外的令牌* [英] NodeJS console SyntaxError: Unexpected token * for generator

查看:63
本文介绍了NodeJS控制台SyntaxError:生成器意外的令牌*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行NodeJS控制台:

I am running the NodeJS console:

$ node --version
v0.12.0

我正在尝试实现一个类似这样的Generator函数

And I am trying to implement a Generator function such as this one

function* colorGen() {
    var colors = ["red", "green", "blue", "white"]
    var i = 0;
    yield colors[i];
    i += 1;
    if (i > 3) {i = 0;}  
}

但是当我运行第一行时,出现语法错误:

But when I run the first line, I get a syntax error:

$ 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后面的节点中可用.请参见已实现的功能在V8中可用,并在节点中可用.

Generator functions are currently available in node behind a flag --harmony_generators. See Features Implemented in V8 and Available in Node.

这篇关于NodeJS控制台SyntaxError:生成器意外的令牌*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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