Eval function usage in Javascript _ javascript skills

Source: Internet
Author: User
Tags javascript eval
Eval () function, which can execute a string as a JavaScript expression. [Eval () function]
JavaScript has many tips to make programming easier.
One of them is the eval () function, which can execute a string as a JavaScript expression.
For example:

The Code is as follows:


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 );


If you run this eval program, you will see that the string "2 + 3" in JavaScript is actually executed.
So when you set the_evaled_answer to eval ("2 + 3"), JavaScript will understand and return the sum of 2 and 3 to the_evaled_answer.
This seems a bit silly, but it can actually make interesting things. For example, you can use eval to directly create a function based on user input.
This allows the program to change the program itself based on time or user input. You can achieve amazing results by taking the opposite picture.
In practice, eval is rarely used, but you may have seen people use eval to obtain objects that are hard to index.
One of the problems with the Document Object Model (DOM) is that sometimes it is a pain to get the object you want.
For example, here is a function that asks the user which image to transform: You can use the following function to change which image:

The Code is as follows:


Function swapOne ()
{
Var the_image = prompt ("change parrot or cheese ","");
Var the_image_object;
If (the_image = "parrot ")
{
The_image_object = Your role Doc ument. parrot;
}
Else
{
The_image_object = plain Doc ument. cheese;
}
The_image_object.src = "ant.gif ";
}


Together with these image tags:
[Img src = "/stuff3a/parrot.gif" name = "parrot"]
[Img src = "/stuff3a/cheese.gif" name = "cheese"]
Note the following statements:
The_image_object = Your role Doc ument. parrot;
It applies an image object to a variable. Although it looks a bit strange, it has no Syntax problems.
But what if you have 100 images instead of two images? You have to write a lot of if-then-else statements. if it works like this:

The Code is as follows:


Function swapTwo ()
{
Var the_image = prompt ("change parrot or cheese ","");
Required parameter Doc ument. the_image.src = "ant.gif ";
}


Unfortunately, JavaScript will look for images named the_image instead of the desired "cheese" or "parrot,
So you get the error message: "I have never heard of an object named the_image ".
Fortunately, eval can help you get the object you want.

The Code is as follows:


Function simpleSwap ()
{
Var the_image = prompt ("change parrot or cheese ","");
Var the_image_name = "zookeeper Doc ument." + the_image;
Var the_image_object = eval (the_image_name );
The_image_object.src = "ant.gif ";
}


If you enter "parrot" in the prompt box, create an example string in the second line, that is, the example Doc ument. parrot. contains the third part of eval.
The line means: "Give me the object named Doc ument. parrot"-that is, the image object you want. Once you obtain this image object, you can
Maybe srcflood is set to ant.gif. A little scared? No. In fact, this is quite useful and is often used by people.
We often go to the Eval function in the middle of Javascript,
Some people think this function is very strange. It can make some strings very powerful.
This function is used when we need to convert a common string into a specific object.
The eval function evaluates a string used as a numeric expression. Its syntax is:
Eval (expr)
Expr is a string parameter evaluated. If the string is an expression, eval evaluates the value of this expression. If this parameter represents one or more JavaScript statements, eval executes these statements. The eval function can be used to convert a date from a format (always string) to a numeric expression or number.
====================================
Eval function
Function: first explain the Javascript code and then execute it.
Usage: Eval (codeString)
CodeString is a string containing Javascript statements. It is compiled using the Javascript engine after eval.
Note:
Example: eval (id + "_ icon. src ="/imgs/collapse_up.gif '");
Id is a previously set parameter, while the string in double quotation marks needs to be compiled.

The Code is as follows:


Function tophide (id) // id indicates menu
{
If (top. topframeset. rows = "31 ,*")
{
Top. topframeset. rows = "86 ,*";
Eval (id + "_ icon. src ="/imgs/collapse_up.gif '");
Eval (id + "_ icon. alt = 'collapse The head '");
Head. style. display = "block"
}
Else
{
Top. topframeset. rows = "31 ,*";
Eval (id + "_ icon. src ="/imgs/collapse_down.gif '");
Eval (id + "_ icon. alt = 'expand The head '");
Head. style. display = "none"
}
}


For more information, see:
Javascript eval () Usage

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.