JavaScript eval () function, javascripteval
Note that the eval () function is a global object of JavaScript. Some people may wonder, isn't it a function? How is it a global object... In JavaScript, functions are objects and objects are defined by functions.
Definition and usage
The eval () function computes a string and executes the JavaScript code.
Syntax
eval(string)
| Parameters |
Description |
| String |
Required. The string to be calculated, which contains the JavaScript expression to be calculated or the statement to be executed. |
Return Value
Returns the string value (if any ).
Description
This method only accepts the original string as the parameter. If the string parameter is not the original string, this method will be returned without any change. Therefore, do not pass a String object as a parameter for the eval () function.
Here is an example:
<Html>
Other eval purposes:
When parsing JSON data, the eval function is used:
- Var txt = '{"employees": [' +
- '{"FirstName": "Bill", "lastName": "Gates"},' +
- '{"FirstName": "George", "lastName": "Bush"},' +
- '{"FirstName": "Thomas", "lastName": "Carter"}]}';
Var obj = eval ("(" + txt + ")");
In newer browsers, native JSON support is provided, and such JSON resolution is faster.
The code here is important
Var obj = eval ("(" + txt + ")")
Change
Var obj = JSON. parse (txt) // This is when the browser is newer
What does javascript eval mean?
The eval function allows dynamic execution of JScript source code. For example, the following code creates a new variable now containing the Date object:
Eval ("var now = new Date ();");
The context when the Code passed to the eval method is executed is the same as that when the eval method is called.
What function does eval represent in javascript?
Eval can execute the string generation Statement, which is similar to SQL exec.
What is the use of eval? Sometimes we don't know what statements to execute in advance, and we only know what statements to execute when conditions and parameters are given. eval will be used in this case. For example:
We need to make a function (). The function is to enter the names of two objects on the webpage, and then the program connects the values of these two objects and outputs them.
Function output (a, B)
{
Var tmpa, tmpb;
Tmpa = document. all. a. value;
Tmpb = document. all. B. value;
Document. write (tmpa + tmpb );
}
Output ('input1', 'input2 ');
In this way, you will be prompted with the error "document. all. a is not an object" and "document. all. B is not an object ". Originally, javascript regards a and B as object names. How can javascript use the values in a as object names? Now we need to use eval and change the code to this:
Function output (a, B)
{
Var tmpa, tmpb;
Tmpa = eval ("document. all." + a + ". value ");
Tmpb = eval ("document. all." + B + ". value ");
Document. write (tmpa + tmpb );
}
Output ('input1', 'input2 ');
In this way, javascript will first extract the values of a and B, and then run them in combination with the previous document. all and later. value, so you can
We successfully retrieved the values of input1 and input2.