Why does
eval('{pickup : new Date(2012, 7, 23, 15, 49, 0, 0)}')
work and
eval('{"pickup" : new Date(2012, 7, 23, 15, 49, 0, 0)}')
does not? I get
Uncaught SyntaxError: Unexpected token :
at <anonymous>:1:1
and yet
{"pickup" : new Date(2012, 7, 23, 15, 49, 0, 0)}
as an object works as expected.
Because {} is interpreted as a block, not as an object literal, which makes pickup a label, not an object key. This is what Javascript sees:
{
pickup:
new Date(2012, 7, 23, 15, 49, 0, 0);
}
If you want Javascript to see this as an object literal, assign it to something or otherwise make it an expression instead of a top-level statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With