1

In the sec 11.1.5 of ECMA-262 we have the object declaration notation:

ObjectLiteral :
    { }
    { PropertyNameAndValueList }
    { PropertyNameAndValueList , }
PropertyNameAndValueList :
    PropertyAssignment
    PropertyNameAndValueList , PropertyAssignment
PropertyAssignment :
    PropertyName : AssignmentExpression
    get PropertyName ( ) { FunctionBody }
    set PropertyName ( PropertySetParameterList ) { FunctionBody }
PropertyName :
    IdentifierName
    StringLiteral
    NumericLiteral
PropertySetParameterList :
    Identifier

Well, consider the following ObjectLiteral: {prop: 'prop'}. Thus we have literal of the form {PropertyName: AssignmentExpression}. Now clearly that 'prop' is AssignmentExpression. By defenition from sec 11.13, AssignmentExpression is

AssignmentExpression :
    ConditionalExpression 
    LeftHandSideExpression = AssignmentExpression 
    LeftHandSideExpression AssignmentOperator AssignmentExpression

Question:

Why 'prop' is AssignmentExpression? There is no AssignmentOperator or = and 'prop' is no ConditionalExpression certanly.

2
  • Surely {prop: 'prop'} is a PropertyNameAndValueList? Commented Dec 19, 2013 at 21:37
  • @lonesomeday I'm confused, can you explain how 'prop' is evaluated in my ObjectLiteral? Commented Dec 19, 2013 at 21:50

3 Answers 3

2

An AssignmentExpression means pretty much any expression in the language, other than ones that use the comma operator/punctuator.

Basically, the production rules have operator precedence and associativity baked in.

 AssignmentExpression 
 => ConditionalExpression                          (11.13)
  => LogicalORExpression                           (11.12)
   => LogicalANDExpression                         (11.11)
    => BitwiseORExpression                         (11.10)
     => BitwiseXORExpression                       (11.10)
      => BitwiseANDExpression                      (11.10)
       => EqualityExpression                       (11.10)
        => RelationalExpression                    (11.9)
         => ShiftExpression                        (11.8)
          => AdditiveExpression                    (11.7)
           => MultiplicativeExpression             (11.6)
            => UnaryExpression                     (11.5)
             => PostfixExpression                  (11.4)
              => LeftHandSideExpression            (11.3)
               => NewExpression                    (11.2)
                => MemberExpression                (11.2)
                 => PrimaryExpression              (11.2)
                  => Literal                       (11.1)
                   => StringLiteral                (7.8)
                    => ' SingleStringChars(opt) '  (7.8.4)

Note how the deeper we go, the higher the precedence. You probably already knew that * has higher precedence than + or -, for example. One very common way of specifying precedence in a grammar is to have a production rule for each tier of operators, and specifically say (for example) that an "additive expression" is one or more "multiplicative expressions" separated by + or -. Given such a rule, the parser would have to handle multiplicative subexpressions before it could even attempt to produce an addition or subtraction expression.

Sign up to request clarification or add additional context in comments.

3 Comments

In the 11.1.5 said that The values need not be literals; they are evaluated each time the object initialiser is evaluated. But as i understood from your post AssignmentExpression can be a Literal
Right. Can be, but doesn't have to be.
Awesome explanation! I'm understand. Many thanks! :)
1

'prop' is no ConditionalExpression certainly.

Yes it is. The term conditional doesn't mean that it necessarily contains a conditional, but that it is at the grammatical level of expressions that are allowed to contain conditionals - to achieve the operator associativity ordering.

Here's a break down:

ConditionalExpression: LogicalORExpression (§11.12)
LogicalORExpression: LogicalANDExpression (§11.11)
LogicalANDExpression: BitwiseORExpression (§11.11)
BitwiseORExpression: BitwiseXORExpression (§11.10)
BitwiseXORExpression: BitwiseANDExpression (§11.10)
BitwiseANDExpression: EqualityExpression (§11.10)
EqualityExpression: RelationalExpression (§11.9)
RelationalExpression: ShiftExpression (§11.8)
ShiftExpression: AdditiveExpression (§11.7)
AdditiveExpression: MultiplicativeExpression (§11.6)
MultiplicativeExpression: UnaryExpression (§11.5)
UnaryExpression: PostfixExpression (§11.4)
PostfixExpression: LeftHandSideExpression (§11.3)
LeftHandSideExpression: NewExpression (§11.2)
NewExpression: MemberExpression (§11.2)
MemberExpression: PrimaryExpression (§11.2)
PrimaryExpression: Literal (§11.1)
Literal: StringLiteral (§7.8)

Comments

0

It's an AssignmentExpression because you're allowed to write:

{ prop: a = 'prop' }

or:

{ prop: a == 3 ? 'prop1' : 'prop2' }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.