WebStorm JavaScript 解构赋值

WebStorm JavaScript destructuring assignment

WebStorm 中的以下有效 ES6:

let {a,b} = {a:0,b:0};
{a,b} = {a:2,b:4};

在第二个等号处显示错误:"expression expected"。很明显JavaScript设置为ES6.

顺便说一下,添加括号可以消除错误:

let {a,b} = {a:0,b:0};
({a,b} = {a:2,b:4});

这是错误还是 ES6 的一部分? node编译器好像第一个版本没有问题(没有括号),所以好像不是标准的一部分。

这不是错误,ExpressionStatement 是这样的:

NOTE An ExpressionStatement cannot start with a U+007B (LEFT CURLY BRACKET) because that might make it ambiguous with a Block. An ExpressionStatement cannot start with the function or class keywords because that would make it ambiguous with a FunctionDeclaration, a GeneratorDeclaration, or a ClassDeclaration. An ExpressionStatement cannot start with async function because that would make it ambiguous with an AsyncFunctionDeclaration. An ExpressionStatement cannot start with the two token sequence let [ because that would make it ambiguous with a let LexicalDeclaration whose first LexicalBinding was an ArrayBindingPattern.

给你的作业加上括号。比如下面是正确的

let foo
({foo = 3} = {foo: 2})