Why does object destructuring throw an error if there is no var
keyword in front of it?
{a, b} = {a: 1, b: 2};
throws SyntaxError: expected expression, got '='
The following three examples work without problems
var {a, b} = {a: 1, b: 2};
var [c, d] = [1, 2];
[e, f] = [1, 2];
Bonus question: Why do we not need a var
for array destructuring?
I ran into the problem doing something like
function () {
var {a, b} = objectReturningFunction();
// Now a and b are local variables in the function, right?
// So why can't I assign values to them?
{a, b} = objectReturningFunction();
}
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…