V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tongchia
V2EX  ›  Node.js

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

  •  
  •   tongchia · 2016-11-08 12:30:11 +08:00 · 3864 次点击
    这是一个创建于 2719 天前的主题,其中的信息可能已经有所发展或是发生改变。
    为啥数组就可以, 比如说


    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 条回复    2016-11-20 10:26:00 +08:00
    lixingcong
        1
    lixingcong  
       2016-11-08 12:33:01 +08:00 via Android
    为什么我从手机 chrome 打开这页面是暗色 css ??
    miyuki
        2
    miyuki  
       2016-11-08 12:34:11 +08:00 via Android
    @lixingcong 节点主题
    7anshuai
        3
    7anshuai  
       2016-11-08 12:34:41 +08:00 via iPhone
    可以参考下 http://node.green
    gucheen
        4
    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
        5
    chemzqm  
       2016-11-08 12:48:39 +08:00
    早就支持了, node 6.9 可用
    smallpath
        6
    smallpath  
       2016-11-09 00:52:34 +08:00
    object rest operator 还没支持,得配 babel + preset-stage3 , 或者转 object rest operator 的插件+stage2
    tongchia
        7
    tongchia  
    OP
       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
        8
    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 支持这种写法?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5253 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 08:30 · PVG 16:30 · LAX 01:30 · JFK 04:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.