gpt4 book ai didi

javascript - NodeJS 控制台语法错误 : Unexpected token * for generator

转载 作者:数据小太阳 更新时间:2023-10-29 05:46:31 25 4
gpt4 key购买 nike

我正在运行 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 后面的 Node 中可用。参见 Features Implemented in V8 and Available in Node .

关于javascript - NodeJS 控制台语法错误 : Unexpected token * for generator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30444366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com