tongchia
V2EX  ›  Node.js

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

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

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