nodejs 怎么使用ES6 中增加的解构?
发布于 10 年前 作者 wangyangkobe 12098 次预览 最后一次回复是 9 年前 来自 问答
function getValue() { return [1, 2]; } var [x, y] = getValue();
console.log(x = ${x}, y = ${y});
居然报错了: var [x, y] = getValue(); ^
SyntaxError: Unexpected token [ at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:374:25) at Object.Module._extensions…js (module.js:405:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Function.Module.runMain (module.js:430:10) at startup (node.js:141:18) at node.js:980:3
使用的node版本是: v5.3.09 回复
http://www.4byte.cn/question/663171/destructuring-in-node-js.html 不支持
用 Babel 编译
不报错才怪呢
https://nodejs.org/en/docs/es6/
用 Babel
我觉得三个点挺好用 function aaa(){ bbb(…arguments) }
如楼上所说,参考 https://nodejs.org/en/docs/es6/
6楼正解,加上特殊标记位~--harmony_destructuring
@reverland 帮了我大忙,思密达
建议参考一下官方文档: https://nodejs.org/en/docs/es6/