5

Destructuring_assignment#Assignment_without_declaration

it says:

the {a, b} on the left-hand side is considered a block and not an object literal.

var a, b;
{a, b} = {a:1, b:2};//Syntax Error!
({a, b} = {a:1, b:2}); // it works

what the '()' do in the second sentence?

why the '{}' in it is considered an object literal?

youzipi
  • 322
  • 4
  • 16
  • Parentheses can only contain expressions: blocks are *statements*, but object literals or variable assignments are *expressions*. – gcampbell Jul 13 '16 at 11:39

4 Answers4

2

Statements must not start with braces in Javascript: Pitfalls of destructuring

Alternatively, the following expression is possible:

"",{a, b} = {a:1, b:2};

It is only important that a statement does not begin with a brace, because code blocks start with one.

  • **Statements must not start with braces** - this is not right. you can execute, for example {}(a = 'test') and it works perfectly. – Andrii S. Jul 13 '16 at 11:54
  • Andrii's comment is wrong. `{}({a, b} = {a:1, b:2});` is either interpreted as an empty object literal or as an empty block (each followed by a destructuring assignment expression). Due to ASI (Automatic Semicolon Insertion) this line is converted to: `{}; ({a, b} = {a:1, b:2});`. That's it. Since neither an object literal nor an empty block itself is a statement, the code is valid. –  Jul 13 '16 at 14:39
2

The first tries to assign a value to a block, which is not right. The second is an equivalent to

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

So here you are calling a constructor, supplying the block of properties and assigning them values.

Andrii S.
  • 236
  • 3
  • 5
1

may I simply cite the hilighted explanation right next to the sample you've linked:

I think it's pretty clear

The ( .. ) around the assignment statement is required syntax when using object literal destructuring assignment without a declaration.

{a, b} = {a:1, b:2} is not valid stand-alone syntax, as the {a, b} on the left-hand side is considered a block and not an object literal.

However, ({a, b} = {a:1, b:2}) is valid, as is var {a, b} = {a:1, b:2}

Community
  • 1
  • 1
Thomas
  • 11,958
  • 1
  • 14
  • 23
0

We can't assign any values to any literals like array,object,string.

ex: [a] = [1];

{a} = {1};

"a" = "b";

But we can assign value using comma separator

[a],{a} = {a:1};

Output:

[a] -- [1]

{a} -- {a:1}

Note:

1.Object literal should not be first in the initialization.

2.In string literal never store any value.

() - is qualto return statement

Whatever you given in {}, it will automatically execute itself;

To check just place the code {return;} anywhere in the function it will return the function.

Just look into code snipped for understanding.

var a =5;b=6;
console.log(JSON.stringify({a,b}));
//Output: {"a":5,"b":6}

[a,b],{a,b} = {"a":1, "b":2};
console.log(JSON.stringify([a,b]));
console.log(JSON.stringify({a,b}));
//Output: 
//[1,2]
//{"a":1,"b":2}

var name = (function(){return "lotus"});
console.log(name);
//Output: function(){return "lotus"}

name = (function(){return "lotus"})();
console.log(name);
//Output: lotus

name = ({a, b} = {a:3, b:4});
console.log(JSON.stringify(name));
//Output: {"a":3,"b":4}