In Firefox console, this code will generate error:
{"d" : ["bankaccountnumber", "$1234.56"] }
> SyntaxError: invalid label {
> message="invalid label", more...}
this code works just fine
{d : ["bankaccountnumber", "$1234.56"] }
> ["bankaccountnumber", "$1234.56"]
this code works fine as well
var a = {'d' : ["bankaccountnumber", "$1234.56"] };
a.d
> ["bankaccountnumber", "$1234.56"]
Can someone help to explain why is the diference? thanks!
In Firefox console, this code will generate error:
{"d" : ["bankaccountnumber", "$1234.56"] }
> SyntaxError: invalid label {
> message="invalid label", more...}
this code works just fine
{d : ["bankaccountnumber", "$1234.56"] }
> ["bankaccountnumber", "$1234.56"]
this code works fine as well
var a = {'d' : ["bankaccountnumber", "$1234.56"] };
a.d
> ["bankaccountnumber", "$1234.56"]
Can someone help to explain why is the diference? thanks!
Share Improve this question edited May 5, 2010 at 16:48 nandin asked May 5, 2010 at 16:37 nandinnandin 2,5755 gold badges24 silver badges28 bronze badges 1- 1 stackoverflow./questions/1509535/javascript-false-and-false/… is a seemingly irrelevant question, but with a relevant answer to one thing you may be tripping on – Crescent Fresh Commented May 5, 2010 at 17:03
2 Answers
Reset to default 6This is because of ambiguous syntax. When you try to make a plain object literal in the first two lines, JavaScript is really interpreting it as a set of braces, then a label, then a statement:
{
d: ["bankaccountnumber", "$1234.56"]
}
This code doesn't evaluate to an object, but just to the array. The first example, you tried to use a string as a label, which is incorrect syntax. The third example works properly, creating an object and storing it in a
.
it is probably having a hard time deciding whether it is an expression or a block. If you use parenthesis around the object it works as it forces an expression. The grouping operator, (
and )
, forces {
and }
to be parsed as object literal.
({"d" : ["bankaccountnumber", "$1234.56"] }) // works
Read Named function expressions demystified. Its not directly related to this issue but does address it when talking about the use of grouping-parenthesis and eval()
.