eval () action:
JS parsed into the string parameter code and run, and returns the execution result;
E.g:
the eval ( " 2 +. 3 " ); // performs its operation and returns the computed value. the eval ( " var age = 10 " ); // declare a variable age
eval scope
A function () { the eval ( " var X =. 1 " ); // equivalent. 1 = X var; the console.log (X); // output. 1 } A (); the console.log (X); // error x is not defined
Description Scope effective in all its range of content
Example 2:
A function () { window.eval ( " var X =. 1 " ); // equivalent to window.x = 1; defines global variables the console.log ( " X " ); // output. 1 } A (); the console.log (X); // output 1
This code tragedy? IE8 in IE8 and what version is not supported.
Solution:
functionA () { IF (window.execScript) { // support IE8 and below versions window.execScript ( " var X =. 1 " ); } the else { // conventional browsers support window.eval ( " var X = . 1 " ); } the console.log (X); } A (); the console.log (X);
Precautions
You should avoid using eval, insecurity, huge performance (2 times, once parsed into js statement, once executed).
Other action
JSON string into a JSON object can be used when eval, for example:
var json ="{name:'Mr.CAO',age:30}"; var jsonObj = eval("("+json")"); console.log(jsonObj);