问题描述
声称,EMCAScript 6解构已在Node.JS中部分实现。我已经尝试了各种例子(使用v0.10.12和 - 和谐
标志),如
This recent video claims that EMCAScript 6 destructuring is already partially implemented in Node.JS. I have tried various examples (using v0.10.12 and the --harmony
flag), such as
var [a, b] = [1, 2];
和
var {a: a, b: b} = {a: 1, b: 2};
无效。 似乎表明V8尚不支持解构。
to no avail. This ticket seems to suggest that destructuring is not yet supported in V8.
是否在Node.JS中部分实现了解构造?什么是我可以玩的代码片段?
Is destructuring really partially implemented in Node.JS? What are snippets of code I can play with?
推荐答案
更新节点v6和更新:节点v6支持解析任务,无需任何特殊需要:
Update for node v6 and newer: Node v6 supports destructuring assignment without anything special needed:
var [a, b] = [1, 2];
对于旧版本的节点:您可以获得支持的和声列表功能通过键入:
For older versions of node: You can get the list of supported harmony features by typing:
node --v8-options | grep harmony
节点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")
您需要的标志,节点4.1中添加了 - harmony_destructuring
。目前,您需要通过 - harmony_destructuring
标志来启用该功能:
The flag you need, --harmony_destructuring
, was added in Node 4.1. Currently, you'll need to pass the --harmony_destructuring
flag to enable the feature:
$ node --harmony_destructuring
> var {foo} = {foo: 'bar'};
undefined
> foo
'bar'
这篇关于Node.JS中的结构化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!