Having semicolon after spread syntax jn javascript breaks execution with error “Unexpected token =”

前端 未结 3 905
栀梦
栀梦 2021-01-15 05:37

Can someone explain me why

3条回答
  •  渐次进展
    2021-01-15 06:11

    This has nothing to do with spread syntax or semicolons.

    Object destructuring assignments that are not preceded with something like var, const, or let must use parentheses (or in some other way occur as a an expression within a larger statement containing it) because otherwise JS will parse the opening brace as the beginning of a block:

    const getabc = ()=>({a:'aa',b:'bb',c:123});
    let a, b, c;
    ({ a, b, c } = {...getabc()});

    At the same time, there is no point in using spread syntax here, so you can remove that:

    const getabc = ()=>({a:'aa',b:'bb',c:123});
    let a, b, c;
    ({ a, b, c } = getabc());
    

提交回复
热议问题