我在node上使用一些ES6新特性,比如解构赋值.
结果却发现一个神奇的现象node --harmony a.js
不起效果
而node --harmony_destructuring a.js
才能起效
比如 我写了一个test.js,内容如下:
var [a, b, c] = [1, 2, 3];
console.log(a,b,c);
很显然,这就是一个实验解构赋值的例子
然后我在命令行里输入node --harmony test.js
报错信息如下:
/Users/chuck7/repository/test/test.js:5
var [a, b, c] = [1, 2, 3];
^
SyntaxError: Unexpected token [
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
如果用node --harmony_destructuring test.js
就可以正确输出
1 2 3
这是神马情况? --harmony不是开启所有harmony选项吗?
注: 以上实验在node v4.4.5 版本上运行
巴扎黑2017-04-17 15:35:55
Thank you for the invitation.
In fact, your question can be found in the official documentation: https://nodejs.org/en/docs/es6/.
The functions of es6 are divided into 3 parts: shipping, staged and in progress.
shipping functions: These functions are already stable. It has been written into node.js, and you can directly use the
staged function: This function is almost completed, but the v8 team has not considered stability, so you need to use the --harmony.
in progress function: this Functions need to be labeled, such as the --harmony_destructuring you wrote above. You can view it through the following command
node --v8-options | grep 'in progress'