您的位置:首页技术文章
文章详情页

node.js - node --harmony不起效果 --harmony_destructuring却可以?

【字号: 日期:2024-07-29 09:35:13浏览:65作者:猪猪

问题描述

我在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:5var [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 版本上运行

问题解答

回答1:

谢谢邀请.其实你的问题可以在官方文档中找到:https://nodejs.org/en/docs/es6/.对于es6的功能分成了3个部分:shipping, staged 和 in progress.shipping功能:这些功能是已经稳定的。已经写入了node.js中的,直接就可以使用staged功能:此功能是几乎完成的功能,但是v8团队没有考虑稳定性,需要使用--harmony.in progress功能: 此功能是需要写出标签的,比如你上面写的--harmony_destructuring.你可以通过下面的命令查看

node --v8-options | grep ’in progress’

node.js - node --harmony不起效果 --harmony_destructuring却可以?

相关文章: