Why is this:
eval('[{\"active":"true"}]')
is OK, but
eval('{"active":"true"}') tells me I'm missing ";" ?
Then why is it that if I put parentheses around the second statement:
eval('({"active":"true"})')
Then it's OK again?
Thanks in advance!
Ray
Duncan Booth - 31 Jul 2006 10:36 GMT
> Why is this:
>
[quoted text clipped - 12 lines]
> Thanks in advance!
> Ray
Because:
[{"active":"true"}]
is a valid expression, but
{"active":"true"}
is a syntax error at the colon.
The first one defines an array containing an object, in the second one
the '{' is starting a block, not defining an object.
See the Ecmascript language specification, section 12.4 Expression
statement:
> Note that an ExpressionStatement cannot start with an opening curly
> brace because that might make it ambiguous with a Block. Also, an
> ExpressionStatement cannot start with the function keyword because
> that might make it ambiguous with a FunctionDeclaration.
Richard Cornford - 31 Jul 2006 10:43 GMT
> Why is this:
>
[quoted text clipped - 9 lines]
>
> Then it's OK again?
A javascript Expression Statement may not commence with an opening
brace, so that it cannot be ambiguous along side Block Statements. The
- eval - function is supposed to execute a string as if it satisfied
the production rules for a Program, and Programs are made up of
statements, so - eval - must see the first opening brace and starting a
Block statement, and then the absence of a semicolon before the colon
is a syntax error. With the parentheses the opening brace is becomes
part of a parenthesised Expression and so must be interpreted as the
start of an object literal, and the parenthesised Expression becomes an
Expression Statement (as no other production can put an opening
parenthesis at the start of a Statement).
Richard.
Ray - 31 Jul 2006 11:12 GMT
Thanks Duncan, Richard, for the explanations!
Cheers,
Ray
> Why is this:
>
[quoted text clipped - 12 lines]
> Thanks in advance!
> Ray