Javascript object destructuring

后端 未结 1 852
有刺的猬
有刺的猬 2020-12-20 03:37

Why is this not valid when using the new es6 destructuring syntax

var a, b, c;
{a, b, c } = {a:1, b:2, c:3};

when this is:

         


        
相关标签:
1条回答
  • In your example, the first { is ambiguous and the parser will interpret it as the beginning of a block. While {a, b, c} is a valid block, the following assignment operator is not valid.

    Wrap everything in parenthesis and it will parse correctly:

    ({a, b, c} = {a:1, b:2, c:3});
    

    Example


    This is similar to having an object literal by itself (for whatever reasons):

    {"a": 42}   // parse error
    ({"a": 42}) // works
    
    0 讨论(0)
提交回复
热议问题