我想了解如何在 Node 中启用 Harmony v8 选项,我的 Node 版本是:
$ 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
我哪里做错了?
最佳答案
显然这是/曾经是 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已于 2015 年 12 月将此问题标记为已修复,因此现在我们只需等待更新的 V8 将其纳入 Node.js 即可。 @mscdex已通知我此修复将在 Node v6.0.0 中提供。
关于javascript - 数组解构赋值在 Node.js 中使用 Harmony 选项在 v8 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36076782/