数组解构赋值在 v8 中无法使用 Node.js 中的和谐选项

Array destructuring assignment not working in v8 with harmony option in Node.js

我想了解如何在 Node 中启用和谐 v8 选项,我的节点版本是:

$ node -v                                                                      
v5.5.0

以ES6解构为例进行测试

$ cat destructure.js
'use strict'
var a, b
[a, b]  = [1, 2] 
console.log(a, b)

运行 如预期的那样直接出错。

$ node destructure.js 
/usr/home/mko_io/pure-js-files/destructure.js:3
[a, b]  = [1, 2]
^^^^^^

但是在设置标志后得到同样的错误:

$ node --harmony_destructuring destructure.js 
/usr/home/mko_io/pure-js-files/destructure.js:3
[a, b]  = [1, 2]
^^^^^^

ReferenceError: Invalid left-hand side in assignment

我哪里做错了?

显然这是 is/was V8 JavaScript 引擎中的错误。

'use strict'
var a, b
[a, b]  = [1, 2] 
console.log(a, b)

不起作用但是...

'use strict'
var [a, b]  = [1, 2] 
console.log(a, b)

在使用 --harmony_destructuring 时有效。

看起来实验性功能尚未完全符合规范。

relevant bug report for V8 marked this issue as fixed in December 2015, so now we just need to wait for the updated V8 to make it into Node. @mscdex 告诉我这个修复将在 Node v6.0.0 中可用。

解构被破坏

In progress features can be activated individually by their respective harmony flag (e.g. --harmony_destructuring), although this is highly discouraged unless for testing purposes.

https://nodejs.org/en/docs/es6/ and this answer Destructuring in Node.JS