tongchia
V2EX  ›  Node.js

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

  •  
  •   tongchia · Nov 8, 2016 · 5055 views
    This topic created in 3657 days ago, the information mentioned may be changed or developed.
    为啥数组就可以, 比如说


    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 下能用吗?
    8 replies  •  2016-11-20 10:26:00 +08:00
    lixingcong
    lixingcong
    Nov 8, 2016 via Android
    1
    为什么我从手机 chrome 打开这页面是暗色 css ??
    miyuki
    miyuki
    Nov 8, 2016 via Android
    2
    @lixingcong 节点主题
    7anshuai
    7anshuai
    Nov 8, 2016 via iPhone
    3
    可以参考下 http://node.green
    gucheen
    gucheen
    Nov 8, 2016
    4
    因为现在 ecma rest operator 不支持 object
    object 的 rest operator 是 stage 3 的规范
    https://github.com/sebmarkbage/ecmascript-rest-spread
    chemzqm
    chemzqm
    Nov 8, 2016
    5
    早就支持了, node 6.9 可用
    smallpath
    smallpath
    Nov 9, 2016
    6
    object rest operator 还没支持,得配 babel + preset-stage3 , 或者转 object rest operator 的插件+stage2
    tongchia
    tongchia
    OP
    Nov 13, 2016
    7
    @smallpath
    @gucheen

    thx~
    而且现在只支持最后一个参数解构 比如
    [a, b, ...others] = ['a', 'b', 'c', 'd', 'e', 'f'] // 这样是可以的
    [a, ...others, lastArg] = ['a', 'b', 'c', 'd', 'e', 'f'] // 这样就不行
    kingze1992
    kingze1992
    Nov 20, 2016
    8
    @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 支持这种写法?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2719 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 10:24 · PVG 18:24 · LAX 02:24 · JFK 05:24
    ♥ Do have faith in what you're doing.