Note that the eval () function is a global object of JavaScript, and some people wonder, not a function, how is it a global object ... Hehe, because functions are objects in JavaScript, objects are defined by function.
Definition and usage
The eval () function computes a string and executes the JavaScript code in it.
Grammar
Eval (String)
parameter |
description |
string |
required. The string to evaluate that contains the JAVASCRIPT expression to be evaluated or the statement to execute. |
return value
The value (if any) that is obtained by calculating the string.
Description
The method only accepts the original string as an argument, and if the string parameter is not the original string, the method returns without any change. Therefore, do not pass a String object as a parameter for the eval () function.
Here are some examples:
Eval Other uses:
The Eval function is used when parsing JSON data:
- var txt  =  " {"Employees": ['
- ' {' firstName ': ' Bill ', ' lastName ': ' Gates '}, ' +
- ' {' firstName ': ' George ', ' lastName ': ' Bush '}, ' +
- ' {' firstName ': ' Thomas ', ' lastName ': ' Carter '}]} ';
var obj = eval ("(" + txt + ")");
< Span style= "font-family:kaiti_gb2312; font-size:24px; Line-height:26px "> In a newer browser, it provides native JSON support, and this JSON parsing is faster.
Here the code is important to put
var obj = eval (" ("+ txt +") "
change to
var obj = JSON.parse (TXT) //This is in the case of a newer browser
The eval () function of JavaScript