Nonlocal Destruction Destination in ES6

var { foo: bar } = { foo: 123 }; works.

{ foo: bar } = { foo: 123 }; no.

How to do the last job when baris a global variable, but the destruction occurs inside the function?

+4
source share
1 answer

As stated in the “Gotcha Syntax” section of understandes6 , you will need to wrap it with parentheses because otherwise it will generate a syntax error. An opening brace is usually the beginning of a block, and blocks cannot be part of an assignment expression.

This one worked for me:

var bar;
({ foo: bar } = { foo: 123 });
console.log(bar); // 123

I also tried:

var bar;
({ foo: bar }) = { foo: 123 };
console.log(bar); // ReferenceError: Invalid left-hand side in assignment at eval

es6lint, , .

+4

All Articles