CNode

nodejs 怎么使用ES6 中增加的解构?

问答
Wwangyangkobe发布于11 年前最后回复10 年前9 回复12276 浏览0 收藏

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)

G
g8up#1·10 年前

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

W
welefen#3·11 年前

用 Babel 编译

C
CoderIvan#4·11 年前
D
DavidCai1993#5·11 年前

用 Babel

J
jiangzhuo#6·11 年前

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

R
reverland#7·11 年前

如楼上所说,参考 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
>
F
fangker#8·10 年前
引用 reverland如楼上所说,参考 https://nodejs.org/en/docs/es6/

@reverland 帮了我大忙,思密达

D
DevinXian#9·11 年前

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

参与回复
登录后即可参与回复。登录