function eval () in JavaScript

Source: Internet
Author: User

The eval () function can execute a string as if it were a JavaScript expression.
We often go to the eval function in the middle of JavaScript,
Some people think that this function is very strange, you can turn some strings to function very powerful
This function is used when we need to turn the normal string into a concrete object.


The Eval function evaluates a string that is a numeric expression whose syntax is:

eval (expr)


Here expr is a string parameter that is evaluated.

If the string is an expression, eval evaluates the value of the expression;

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 a format (always a string) to a numeric expression or a number.

Example one:
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 are" + 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 the 2 and 3 and back to The_evaled_answer.

This seems a bit silly, actually can make very interesting things. Using eval, for example, you can create functions directly from the user's input.
This allows the program to change itself depending on time or user input, and with extrapolate, you can get amazing results.
In practice, eval is seldom used, but you may have 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's painful to get the object you're asking for.


Example two: Here is a function that asks the user which image to transform: Transform which image you can use the following function:
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";
}

Note the following lines of statements like this:
The_image_object = Window.document.parrot;
It puts an image object into a variable. It seems a little strange, but it has no grammatical problems.
But what happens when you have 100 images instead of two? You have to write a whole bunch of if-then-else statements, if you can do it like this:
function Swaptwo ()
{
var the_image = prompt ("Change parrot or Cheese", "");
WINDOW.DOCUMENT.THE_IMAGE.SRC = "Ant.gif";
}
Unfortunately, JavaScript will look for an image named The_image instead of the "cheese" or "parrot" you want,
So you get the error message: "I've 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 = "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 is created in the second line, which is Window.document.parrot. Then included the third of the Eval
The line means: "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. In fact, this is very useful, and people often use it.

Example three:

var struser = ' {' name ': ' Zyp ', ' age ': ' Ten ', ' Sex ': ' Woman '} ';

var objuser = eval (struser);

var strName = Objuser.name;

If you want to invoke the user's Name property, you might easily think of JSON. But struser is a string, so the string is first converted to a JSON object, and then it is convenient to call the user's property to get the value of the property.

function eval () in JavaScript

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.