The Eval function usage in JS detail

Source: Internet
Author: User
Tags eval

Definitions 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, which contains the JAVASCRIPT expression to evaluate or the statement to execute.

return value

The value, if any, obtained by calculating the string.

Description

The method accepts only the original string as a parameter, and if the string argument is not the original string, the method returns without any changes. Therefore, do not pass a String object as an argument for the eval () function.

If you attempt to overwrite the Eval attribute or assign the eval () method to another property and call it through this property, the ECMAScript implementation allows a Evalerror exception to be thrown.

Thrown

Throws an SyntaxError exception if there are no valid expressions and statements in the argument.

If Eval () is invoked illegally, a Evalerror exception is thrown.

If the Javascript code passed to eval () generates an exception, eval () passes the exception to the caller.

Tips and comments
Tip: Although the eval () function is very powerful, it is not much used in practice

As a small example:

The code is as follows Copy Code
Executing an expression
var the_unevaled_answer = "2 + 3";
var the_evaled_answer = eval ("2 + 3");
Alert ("The un-evaled answer is" + The_unevaled_answer + "and the evaled answer is" + the_evaled_answer);


Cases

The code is as follows Copy Code

<script language= "JavaScript" >
function Showsubmenu (SID)
{
Whichel = eval ("submenu" + SID);
if (WhichEl.style.display = "None")
{
Eval ("submenu" + Sid + ". style.display=" ";");
}
Else
{
Eval ("submenu" + Sid + ". style.display=" None ";");
}
}
</SCRIPT>

We're going to do a function (), which is to enter the names of two objects in the Web page, and then the program joins the values of the two objects and outputs them.

The code is as follows Copy Code
function Output (a,b)
            {
                var tmpa,tmpb;
               tmpa= Document.all.a.value;
               tmpb= Document.all.b.value;
               document.write (tmpa+ TMPB);
           }
            output (' input1 ', ' input2 ');


This way you will be prompted with the error "DOCUMENT.ALL.A is not an object" and "document.all.b is not an object". It turns out that JavaScript takes A and B as an object name, how can javascript make the value in a the object name? It's time to use eval and change the code like this:

The code is as follows Copy Code
             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 ');

This allows JavaScript to first remove the A,b value and then run it with the previous document.all, and then the. Value combination, so you can
Smooth out the values of INPUT1 and Input2

Personal summary of the Eval function

Here expr is a string parameter that is evaluated. If the string is an expression, Eval asks for the value of the expression, and if the parameter represents one or more JavaScript statements, then Eval executes the statements. The Eval function can be used to convert a date from one format (always a string) to a numeric expression or a number

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.