demo1:
{"aa":111}
demo2:
{aa:111}
demo1 result:
SyntaxError: Unexpected token : (in chrome)
demo2 result:
111
how to explain these two demos?
tks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They’re being parsed as statements, not expressions.
The
{ ... }is parsed as a block statement.The
aa:is parsed as a statement label.The
111is parsed as an expression statement containing a number. Since it’s the last statement you’re eval-ing, its value is returned.The
"aa":is a syntax error. Since statement labels cannot contain"s, it’s parsed as an expression statement (like111, but a string rather than a number). Therefore, the"makes no sense.You want them to be parsed as object literals, which are expressions.
Wrap them in parentheses.