Node 还不支持从 Object 中 用 ... 解构 ?

2016-11-08 12:30:11 +08:00
 tongchia
为啥数组就可以, 比如说


let [x, ...y] = ['a', 'b', 'c'];
// => x = 'a'
// => y = ['b', 'c']
// OK 啊~
let {foo, ...oths} = {foo: 'bar', x: 1, y: 2, z: 3}
// SyntaxError: Unexpected token ...


参数也是可以的

const fn = (x, ...oths) => {console.log(oths);}

为啥 Object 不可以 7.0 下能用吗?
3874 次点击
所在节点    Node.js
8 条回复
lixingcong
2016-11-08 12:33:01 +08:00
为什么我从手机 chrome 打开这页面是暗色 css ??
miyuki
2016-11-08 12:34:11 +08:00
@lixingcong 节点主题
7anshuai
2016-11-08 12:34:41 +08:00
可以参考下 http://node.green
gucheen
2016-11-08 12:38:16 +08:00
因为现在 ecma rest operator 不支持 object
object 的 rest operator 是 stage 3 的规范
https://github.com/sebmarkbage/ecmascript-rest-spread
chemzqm
2016-11-08 12:48:39 +08:00
早就支持了, node 6.9 可用
smallpath
2016-11-09 00:52:34 +08:00
object rest operator 还没支持,得配 babel + preset-stage3 , 或者转 object rest operator 的插件+stage2
tongchia
2016-11-13 17:26:16 +08:00
@smallpath
@gucheen

thx~
而且现在只支持最后一个参数解构 比如
[a, b, ...others] = ['a', 'b', 'c', 'd', 'e', 'f'] // 这样是可以的
[a, ...others, lastArg] = ['a', 'b', 'c', 'd', 'e', 'f'] // 这样就不行
kingze1992
2016-11-20 10:26:00 +08:00
@tongchia Nicholas Zakas 大神的书中有写:

Rest items must be the last entry in the destructured array and cannot be followed by a
comma. Including a comma after rest items is a syntax error.

所以你最后的 destructure 用的不对。
难道 ES2016 支持这种写法?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/318961

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX