JavaScript eval () function, javascripteval

Source: Internet
Author: User

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:

  1. Var txt = '{"employees": [' +
  2. '{"FirstName": "Bill", "lastName": "Gates"},' +
  3. '{"FirstName": "George", "lastName": "Bush"},' +
  4. '{"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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.