_javascript tips for using Eval functions in JavaScript

Source: Internet
Author: User
Tags javascript eval
The eval () function
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.
As a small example:
Copy Code code 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 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:
[img src= "/stuff3a/parrot.gif" Name= "Parrot"]
[img src= "/stuff3a/cheese.gif" name= "cheese"]
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: "I'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 Eval's third
"Give me the object Window.document.parrot"--that is, the image object you want. Once you have acquired this image object, you can
Its src attribute is set to ant.gif. A little scared? Don't. This is actually quite useful and people often use it.
We often go to the eval function in the middle of JavaScript,
Some people think that this function is very strange, can make some strings become very powerful
This function is used when we need to convert a normal string into a specific object.
The Eval function is evaluated as a string of numeric expressions, and its syntax is:
eval (expr)
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.
==============================
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.
Comments:
Example: eval (id + "_icon.src="/imgs/collapse_up.gif ' ");
The ID is the previously set parameter, and the string in double quotes is the one that needs to be compiled
Copy Code code 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");
Head.style.display = "Block"
}
Else
{
Top.topframeset.rows = "31,*";
Eval (id + "_icon.src="/imgs/collapse_down.gif ');
Eval (id + "_icon.alt= ' Expand");
Head.style.display = "None"
}
}

More can refer to:
JavaScript eval () usage
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.