Eval function usage in Javascript

Source: Internet
Author: User

Javascript has many tips to make programming easier. One of them is the eval () function, which can execute a string as a javascript expression. The following is its description

Eval function
Function: Explains JavaScript first.CodeAnd then execute it
Usage: eval (codestring)
Codestring is a string containing JavaScript statements. It is compiled using the JavaScript engine after eval.

For example:

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 evalProgram, 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:

 

Function Swapone ()
{
VaR The_image = Prompt ( " Change parrot or cheese " , "" );
VaR The_image_object;

If(The_image= "Parrot")
{
The_image_object=Optional parameter Doc ument. Parrot;
}
Else
{
The_image_object=Zookeeper 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 = Optional parameter 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:

Function Swaptwo ()
{
VaR The_image = Prompt ( " Change parrot or cheese " , "" );
Zookeeper Doc ument. the_image.src = " Ant.gif " ;
}

Unfortunately, JavaScript will look for an image 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.

 

Function Simpleswap ()
{
VaR The_image = Prompt ( " Change parrot or cheese " , "" );
VaR The_image_name = " Invalid parameter 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 a serial parameter Doc ument. parrot in the second line, and then include the third line of Eval, which means: "specify Doc ument. parrot"-that is, the image object you want. Once you get this picture, you can set your src as ant.gif. A little scared? No. In fact, this is quite useful and is often used by people.

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.