Is there any reason for which
eval("(function(x){return x*x;})")
doesn't return a function with IE9?
eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension.
The eval server command has been deprecated since MongoDB 3.0 and is definitely not recommendable for performance or security reasons.
Answer: eval is a built-in- function used in python, eval function parses the expression argument and evaluates it as a python expression. In simple words, the eval function evaluates the “String” like a python expression and returns the result as an integer.
The return value of eval() is the value of last expression evaluated, if it is empty then it will return undefined . Example: eval("console.
It's a bug in the JScript parser. It shouldn't happen in IE9 unless you are in a Compatibility or Quirks mode.
IE8 misinterprets your function expression as a function declaration, causing it to fail to return anything. Previously mentioned in this question.
You can work around it using one of the other typical ways to unconfuse the JScript parser on what constitutes an expression vs a statement, eg:
eval('[function(x){return x*x;}][0]')
eval('0?0:function(x){return x*x;}')
Due to a parser bug, IE's eval
cannot directly evaluate a function.
Instead, you should use the Function
constructor:
new Function("x", "return x * x;")
Alternatively, you can wrap the eval'd expression in an array:
eval("[ function(x){return x*x;} ][0]")
IE doesn't object to eval
ing an array or object that contains a function.
eval("(function(x){return x*x;})")
...works just fine in IE9 in normal mode (live proof) (but see below). Doesn't mean it's a good idea (very, very few uses of eval
qualify as anything other something than to be avoided), but it does work.
It also works (and should work) in recent versions of Chrome, Firefox, and Opera.
It doesn't seem to work in IE6, IE7, IE8, or IE9 in "compatibility view". That would be a bug (apparently one that's fixed along with about a thousand others with the newer JavaScript engine in IE9), there's nothing wrong with the expression.
You can fix it in earlier versions by forcing the parser to realize it's an expression (live copy):
eval("(0 || function(x){return x*x;})")
I just tested it and
eval("(function(x){return x*x;})")
it returns a function in IE9.
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