Why can't a line like "{opacity: 1.0, width: '132px'}"
be evaluated using eval () as it is?
Since the text occurs where the operator or block is expected, not the expression, and therefore {
denotes the beginning of the block, and not the beginning of the object initializer . (And then opacity:
interpreted as a label , followed by an operator separator [comma], and then width:
looks like another label that is invalid there.)
Putting in parentheses changes the parsing context so that an expression is expected, and therefore {
opens the initializer. (For the same reason, you see self-signed anonymous functions enclosed in parentheses, for example (function(){ ... })();
and not just function(){ ... }();
)
source share