How does the JavaScript Console take a string that contains a object and convert it to a actual JavaScript object?

19 views
Skip to first unread message

Chris Gregory

unread,
Jun 22, 2016, 12:51:47 PM6/22/16
to Google Chrome Developer Tools
If you take the string: 
{key:'1234',hash:'456'}
and you paste it into the console and press enter, the console evaluates the string and turns it into an expandable object. How does it do this?

I have tried to replicate this in my Javascript code by just setting a variable from that string using eval() and JSON.parse() which both throw syntax errors.

Can I get some insight into how this function is implemented?

Thank you.

PhistucK

unread,
Jun 22, 2016, 12:55:46 PM6/22/16
to Google Chrome Developer Tools
Looks like there is little magic here which is revealed when you press up. The expression is wrapped in parentheses, which makes it work.

By the way, JSON.parse does not parse it because it expects double-quoted strings and not bare keys, or even single-quoted strings. If you had used double quotes everywhere, it would have parsed it. eval still would not, due to the lack of parentheses.


PhistucK

--
You received this message because you are subscribed to the Google Groups "Google Chrome Developer Tools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-chrome-develo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-chrome-developer-tools/32b68509-c3b7-4712-9cd6-97f20cce03e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages