Node.js 支持 =>(箭头函数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19644341/
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-09-02 16:06:55  来源:igfitidea点击:

Node.js support for => (arrow function)

javascriptnode.js

提问by exebook

Is Node.js supporting => function keyword alias already? If yes, starting from which version? How to enable this language extension?

Node.js 是否已经支持 => 函数关键字别名?如果是,从哪个版本开始?如何启用此语言扩展?

(function() { console.log('it works!') })()

Becomes

成为

(() => { console.log('it works!') })()

采纳答案by Valentyn Shybanov

You can follow this issue: https://code.google.com/p/v8/issues/detail?id=2700

你可以关注这个问题:https: //code.google.com/p/v8/issues/detail?id=2700

Currently (as 02.05.2014) arrow functions have been implemented and waiting until this functionality will be landed in v8: https://codereview.chromium.org/160073006/

目前(截至 2014 年 5 月 2 日)箭头功能已经实现并等待此功能将在 v8 中登陆:https: //codereview.chromium.org/160073006/

After then we'll need to wait, until v8 version with arrow function would be integrated into Node.JS. You can follow Node.JS changelog there: https://github.com/joyent/node/blob/master/ChangeLog(search for "v8: upgrade to ....")

之后我们需要等待,直到带有箭头功能的v8版本被集成到Node.JS中。你可以在那里关注 Node.JS 更新日志:https: //github.com/joyent/node/blob/master/ChangeLog(搜索“v8: upgrade to ....”)

回答by Diego V

In short: yes, arrow functions are reasonably well supported in Node.js since version 4.4.5.

简而言之:是的,自 4.4.5 版以来,Node.js 中的箭头函数得到了相当好的支持。

Completely correct support starts with version 6. Initial support was introduced as far as v0.12 but is was very incomplete and disabled by default until v4.0 when it got better. See Node's ES6 compatibility table for details: http://node.green/#ES2015-functions-arrow-functions.

完全正确的支持从版本 6 开始。最初的支持是在 v0.12 中引入的,但在 v4.0 变得更好之前,它非常不完整并且默认禁用。有关详细信息,请参阅 Node 的 ES6 兼容性表:http: //node.green/#ES2015-functions-arrow-functions

回答by apsillers

The syntax you're referring to is "arrow function" syntax. It is a feature of ECMAScript 6, also known as "Harmony". The ES6 standard is now finalized, but engines are still implementing its new features.

您所指的语法是“箭头函数”语法。它是 ECMAScript 6 的一个特性,也被称为“和谐”。该ES6标准现在敲定,但发动机仍在执行它的新功能。

The V8 now has arrow function support. Node runs on the V8 engine, but it can take some time for Node to incorporate the latest version into its code base.

V8 现在支持箭头功能。Node 在 V8 引擎上运行,但 Node 可能需要一些时间才能将最新版本合并到其代码库中。

Whenever it is added, it might possibly be enabled only via a --harmonycommand-line flag.

每当添加它时,它可能只能通过--harmony命令行标志启用。

回答by joeytwiddle

kangax's compatibility tablescan keep you up-to-date with what is currently available in Node.

kangax 的兼容性表可以让您及时了解 Node.js中当前可用的内容。

Experimental features can be enabled using the instructions on this page:

可以使用此页面上的说明启用实验性功能:

All shipping features are turned on by default on Node.js

Staged feature require a runtime flag: --es_staging(or its synonym, --harmony)

In progress features can be activated individually by their respective harmony flag (e.g. --harmony_destructuring) but this is highly discouraged

默认情况下,Node.js 上的所有运输功能都处于启用状态

暂存功能需要运行时标志:(--es_staging或其同义词,--harmony

进行中的功能可以通过它们各自的和谐标志(例如--harmony_destructuring)单独激活,但这是非常不鼓励的