Nodejs 还不支持从 Object 中 用 ... 解构 ?
Nodejs 还不支持从 Object 中 用 … 解构 ?
为啥数组就可以, 比如说
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 下能用吗?
节点主题
可以参考下 http://node.green
因为现在 ecma rest operator 不支持 object
object 的 rest operator 是 stage 3 的规范
https://github.com/sebmarkbage/ecmascript-rest-spread
早就支持了, node 6.9 可用
object rest operator 还没支持,得配 babel + preset-stage3 , 或者转 object rest operator 的插件+stage2
thx~
而且现在只支持最后一个参数解构 比如
[a, b, …others] = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’] // 这样是可以的
[a, …others, lastArg] = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’] // 这样就不行
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 支持这种写法?