I have this code example:
var a = 10;
({a}) = 0;
In Google Chrome, it shows an error: SyntaxError: Invalid destructuring assignment target
In Firefox, it shows an error: ReferenceError: invalid assignment left-hand side
Actually, I would like to understand what kind of error it is?
I have this code example:
var a = 10;
({a}) = 0;
In Google Chrome, it shows an error: SyntaxError: Invalid destructuring assignment target
In Firefox, it shows an error: ReferenceError: invalid assignment left-hand side
Actually, I would like to understand what kind of error it is?
Share Improve this question edited Mar 2, 2019 at 20:51 MaximPro asked Mar 2, 2019 at 20:49 MaximProMaximPro 5421 gold badge8 silver badges24 bronze badges 10- What did you expect that code to do? – Pointy Commented Mar 2, 2019 at 20:49
- @Pointy just testing the language – MaximPro Commented Mar 2, 2019 at 20:50
-
2
Well the language is telling you that that's wrong :) The
( )
are wrong, and the right-hand side is not an object so that's wrong too. – Pointy Commented Mar 2, 2019 at 20:52 - Because you're using an incorrect syntax: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Terry Commented Mar 2, 2019 at 20:54
- It's still interesting that both browsers throw different errors. – Jonas Wilms Commented Mar 2, 2019 at 20:56
1 Answer
Reset to default 4Well, it's simply invalid syntax. You are not allowed to put a destructuring pattern in parentheses[1]:
It is an early Reference Error[2] if LeftHandSideExpression is neither an ObjectLiteral nor an ArrayLiteral and IsValidSimpleAssignmentTarget of LeftHandSideExpression is
false
.
The ({a})
you have as a LeftHandSideExpression is a ParenthesizedExpression, not an ObjectLiteral, and the parenthesis don't contain a simple assignment target.
You probably are looking for a parenthesised statement to allow the destructuring pattern:
var a = 10;
({a} = 0);
1: Surprisingly, (a) = 0;
is a valid statement though.
2: It seems that Chrome is wrong by throwing a SyntaxError
then
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744319542a4568346.html
评论列表(0条)