Extra content for "Understanding the ECMAScript spec, part 2"
Why is o2.foo
an AssignmentExpression
?
o2.foo
doesn’t look like an AssignmentExpression
since there’s no assignment. Why is it an AssignmentExpression
?
The spec actually allows an AssignmentExpression
both as an argument and as the right hand side of an assignment. For example:
function simple(a) {
console.log('The argument was ' + a);
}
simple(x = 1);
// → Logs “The argument was 1”.
x;
// → 1
…and…
x = y = 5;
x; // 5
y; // 5
o2.foo
is an AssignmentExpression
which doesn't assign anything. This follows from the following grammar productions, each one taking the "simplest" case until the last one:
An AssignmentExpresssion
doesn't need to have an assignment, it can also be just a ConditionalExpression
:
(There are other productions too, here we show only the relevant one.)
A ConditionalExpression
doesn't need to have a conditional (a == b ? c : d
), it can also be just a ShortcircuitExpression
:
And so on:
ShortCircuitExpression : LogicalORExpression
LogicalORExpression : LogicalANDExpression
LogicalANDExpression : BitwiseORExpression
BitwiseORExpression : BitwiseXORExpression
BitwiseXORExpression : BitwiseANDExpression
BitwiseANDExpression : EqualityExpression