I’ve learned (the hard way) that I need to add parentheses around JSON data, like this:
stuff = eval('(' + data_from_the_wire + ')');
// where data_from_the_wire was, for example {"text": "hello"}
(In Firefox 3, at least).
What’s the reason behind this? I hate writing code without understanding what´s behind the hood.
Putting the parentheses around
data_from_the_wireis effectively equivalent toIf you were to eval without the parentheses, then the code would be evaluated, and if you did have any named functions inside it those would be defined, but not returned.
Take as an example the ability to call a function just as it han been created:
Will call the function that has just been defined. The following, however, does not work:
So we see that the parentheses effectively turn then code into an expression that returns, rather than just code to run.