nodejs 怎么使用ES6 中增加的解构?
发布于 8 年前 作者 wangyangkobe 11369 次浏览 来自 问答

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.0
9 回复

用 Babel 编译

我觉得三个点挺好用 function aaa(){ bbb(…arguments) }

如楼上所说,参考 https://nodejs.org/en/docs/es6/

┌─[reverland@马赛克马赛克] - [~] - [2015-12-24 02:15:07]
└─[0] <> node --harmony_destructuring
> function getValue() { return [1, 2]; } var [x, y] = getValue();
undefined
> x
1
> y
2
>

6楼正解,加上特殊标记位~--harmony_destructuring

@reverland 帮了我大忙,思密达

建议参考一下官方文档: https://nodejs.org/en/docs/es6/

回到顶部