The detailed usage and description of the Eval function in JavaScript _javascript tips

Source: Internet
Author: User
Tags eval
JavaScript has a number of tips to make programming easier. One of these is the eval () function, which can execute a string as a JavaScript expression. Here is a description of it
Eval function
Function: First explain the JavaScript code, and then execute it
Usage: Eval (codestring)
Codestring is a string containing JavaScript statements that are compiled using the JavaScript engine after Eval.

As a small 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);
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

If you run this eval program, you will see that the string "2 + 3" in JavaScript is actually executed. So when you set the value of The_evaled_answer to eval ("2 + 3"), JavaScript will understand and return 2 and 3 and back to The_evaled_answer.
This may seem a bit silly, but it can actually do very interesting things. Like using eval, you can create a function directly from the user's input. This allows the program to change depending on the time or user input, and you can get amazing results by extrapolate.
In practice, eval is rarely used, but perhaps you've seen someone use eval to get objects that are difficult to index. One of the problems with the Document Object Model (DOM) is that sometimes it is painful to get the object you ask for. For example, here's a function that asks the user which image to transform: Which image you want to transform-you can use the following function:

Copy Code code as follows:

function Swapone ()
{
var the_image = prompt ("Change parrot or Cheese", "");
var the_image_object;
if (the_image = = "Parrot")
{
The_image_object = Window.document.parrot;
}
Else
{
The_image_object = Window.document.cheese;
}
THE_IMAGE_OBJECT.SRC = "Ant.gif";
}

Along with these image tags:




Notice several lines of statements like this:

The_image_object = Window.document.parrot;
It compresses an image object to a variable. Strange as it may seem, it has no grammatical problems. But what if you have 100 images instead of two? You have to write a lot of if-then-else statements, if you can like this:
Copy Code code as follows:

function Swaptwo ()
{
var the_image = prompt ("Change parrot or Cheese", "");
WINDOW.DOCUMENT.THE_IMAGE.SRC = "Ant.gif";
}

Unfortunately, JavaScript will look for images called the_image rather than "cheese" or "parrot" you want, so you get the wrong message: "You've never heard of an object named The_image."
Fortunately, Eval can help you get the object you want.

Copy Code code as follows:

function Simpleswap ()
{
var the_image = prompt ("Change parrot or Cheese", "");
var the_image_name = "window.document." + the_image;
var the_image_object = eval (the_image_name);
THE_IMAGE_OBJECT.SRC = "Ant.gif";
}

If the user fills in the "parrot" in the Prompt box, a string that is Window.document.parrot is created in the second line. And then contains the third line of eval meaning "give me the object Window.document.parrot"-The image object you want. Once you have acquired this image object, you can set its SRC attribute to ant.gif. A little scared? Don't. This is actually quite useful and people often use it.
Related Article

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.