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
Almost there…
ShiftExpression : AdditiveExpression
AdditiveExpression : MultiplicativeExpression
Don’t despair! Just a couple of more productions…
Then we hit the productions for LeftHandSideExpression:
LeftHandSideExpression :NewExpressionCallExpressionOptionalExpression
It’s not clear which production might apply to o2.foo. We just need to know (or find out) that a NewExpression doesn’t actually have to have the new keyword.
MemberExpression sounds like something we were looking for, so now we take the production
So, o2.foo is a MemberExpression if o2 is a valid MemberExpression. Luckily it's much easier to see:
MemberExpression : PrimaryExpression
o2 is surely an Identifier so we're good. o2 is a MemberExpression, so o2.foo is also a MemberExpression. A MemberExpression is a valid AssignmentExpression, so o2.foo is an AssignmentExpression too.