在 Node.JS 中解构

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

Destructuring in Node.JS

javascriptnode.jsecmascript-6destructuring

提问by Randomblue

This recent videoclaims that EMCAScript 6 destructuring is already partially implemented in Node.JS. I have tried various examples (using v0.10.12 and the --harmonyflag), such as

最近的这个视频声称 EMCAScript 6 解构已经在 Node.JS 中部分实现。我尝试了各种示例(使用 v0.10.12 和--harmony标志),例如

var [a, b] = [1, 2];

and

var {a: a, b: b} = {a: 1, b: 2};

to no avail. This ticketseems to suggest that destructuring is not yet supported in V8.

无济于事。这张票似乎表明 V8 尚不支持解构。

Is destructuring really partially implemented in Node.JS? What are snippets of code I can play with?

解构真的在 Node.JS 中部分实现了吗?我可以使用哪些代码片段?

回答by Laurent Perrin

Update for node v6 and newer: Node v6 supports destructuring assignment without anything special needed:

节点 v6 和更新版本的更新:节点 v6 支持解构赋值,无需任何特殊需要:

var [a, b] = [1, 2];

For older versions of node: You can get the list of supported harmony features by typing:

对于旧版本的 node:您可以通过键入以下内容获取支持的和声功能列表:

node --v8-options | grep harmony

node --v8-options | grep harmony

node 5.x will give you:

节点 5.x 会给你:

--es_staging (enable all completed harmony features)
--harmony (enable all completed harmony features)
--harmony_shipping (enable all shipped harmony fetaures)
--harmony_modules (enable "harmony modules" (in progress))
--harmony_regexps (enable "harmony regular expression extensions" (in progress))
--harmony_proxies (enable "harmony proxies" (in progress))
--harmony_sloppy_function (enable "harmony sloppy function block scoping" (in progress))
--harmony_sloppy_let (enable "harmony let in sloppy mode" (in progress))
--harmony_unicode_regexps (enable "harmony unicode regexps" (in progress))
--harmony_reflect (enable "harmony Reflect API" (in progress))
--harmony_destructuring (enable "harmony destructuring" (in progress))
--harmony_default_parameters (enable "harmony default parameters" (in progress))
--harmony_sharedarraybuffer (enable "harmony sharedarraybuffer" (in progress))
--harmony_atomics (enable "harmony atomics" (in progress))
--harmony_simd (enable "harmony simd" (in progress))
--harmony_array_includes (enable "harmony Array.prototype.includes")
--harmony_tostring (enable "harmony toString")
--harmony_concat_spreadable (enable "harmony isConcatSpreadable")
--harmony_rest_parameters (enable "harmony rest parameters")
--harmony_sloppy (enable "harmony features in sloppy mode")
--harmony_arrow_functions (enable "harmony arrow functions")
--harmony_new_target (enable "harmony new.target")
--harmony_object_observe (enable "harmony Object.observe")
--harmony_spreadcalls (enable "harmony spread-calls")
--harmony_spread_arrays (enable "harmony spread in array literals")
--harmony_object (enable "harmony Object methods")

The flag you need, --harmony_destructuring, was added in Node 4.1. Currently, you'll need to pass the --harmony_destructuringflag to enable the feature:

您需要的标志--harmony_destructuring是在 Node 4.1 中添加的。目前,您需要传递--harmony_destructuring标志以启用该功能:

$ node --harmony_destructuring
> var {foo} = {foo: 'bar'};
undefined
> foo
'bar'

回答by birnbaum

The lately released node.js v6is using V8 version 5.0 which is supporting93% of ES2015 language features (even 96% in v6.1).

最近发布的node.js v6使用的是 V8 5.0 版本,它支持93% 的 ES2015 语言特性(甚至在 v6.1 中支持96%)。

Destructuring assignments can now be considered stable and can be used without any flags.

解构赋值现在可以被认为是稳定的,并且可以在没有任何标志的情况下使用。

回答by Dan Dascalescu

The ES6 compatibility tableshows that destructuring isn't supported in either Chrome 45, or Node v4.

所述ES6兼容性表显示,拆解未在任一铬45,或节点V4的支持。