Javascript 箭头函数“预期表达式”语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35550409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:52:16  来源:igfitidea点击:

Arrow function "expression expected" syntax error

javascriptnode.jsecmascript-6arrow-functions

提问by Milkncookiez

I want to transform this code:

我想转换这段代码:

var formatQuoteAmount = function (tx) {
    return Currency.toSmallestSubunit(tx.usd, 'USD');
};
var quoteAmounts = res.transactions.map(formatQuoteAmount);

into an anonymous arrow function. I've written this:

变成匿名箭头函数。我写过这个:

var quoteAmounts = res.transactions.map(tx => Currency.toSmallestSubunit(tx.usd, 'USD'));

I get expression expectedsyntax error at the arrow. I looked up the default syntax hereand seems like the syntax of my code is correct. Any ideas what the problem might be?

expression expected在箭头处收到语法错误。我在这里查找了默认语法,似乎我的代码的语法是正确的。任何想法可能是什么问题?

I have it working with this syntax:

我让它使用这种语法:

    var quoteAmounts = res.transactions.map(function (tx) {
        return Currency.toSmallestSubunit(tx.usd, 'USD')
    });

but I want to make it a one-liner, with an arrow-function.

但我想让它成为一个单线,带有箭头功能。

Running on node v5.3.0

继续运行 node v5.3.0

回答by Joe23

I had the error expression expectedreported by Webstormwhen editing a Node.js program. In this case the solution is to set the language version to a version that supports this feature.

我在编辑 Node.js 程序时expression expected收到了Webstorm报告的错误。在这种情况下,解决方案是将语言版本设置为支持此功能的版本。

enter image description here

在此处输入图片说明

回答by Odeyinka Olubunmi

The following is what i did that work for me. (1) I change the JavaScript language option to ECMAScript 6 as show in the selected answer by @Joe23

以下是我为我所做的工作。(1) 我将 JavaScript 语言选项更改为 ECMAScript 6,如@Joe23 选择的答案所示

(2) I close the Webstorm project/application.

(2) 我关闭了 Webstorm 项目/应用程序。

(3) Navigate to the project folder and delete the .ideafolder in it. I believe this is the folder webstorm generated to keep information about the project/application.

(3) 导航到项目文件夹,删除其中的.idea文件夹。我相信这是 webstorm 生成的文件夹,用于保存有关项目/应用程序的信息。

(4) I reopen my project in webstorm and the errors are gone.

(4) 我在 webstorm 中重新打开我的项目,错误消失了。

回答by sitifensys

Your syntax is correct and Nodejs supports arrow functions, but you it's not enabled by default.

您的语法是正确的,Nodejs 支持箭头函数,但默认情况下未启用。

You should add the "--harmony" flag when you start the node process to enable it.

您应该在启动节点进程时添加“--harmony”标志以启用它。