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: 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.
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 );
For example
If you run this eval program, you will see the string "2 +
3 "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 = Your role Doc ument. parrot;
}
Else
{
The_image_object = plain Doc ument. cheese;
}
The_image_object.src = "ant.gif ";
}
Together with these image tags:
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:
Function swapTwo ()
{
Var the_image = prompt ("change parrot or cheese ","");
Required parameter 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 = "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 a temporary string named "parameter Doc ument. parrot" in the second line.
The third line that contains eval means: "Give me the objective Doc ument. parrot "-
That is, the image object you want. Once you get this example, you can set your src as ant.gif.
A little scared? No. In fact, this is quite useful and is often used by people.