Javascript eval function parsing post)

Source: Internet
Author: User
Tags javascript eval

Guidance:
Eval ()Function
This is a simple eval article I have found. I post it here and add some of my experiences! Thanks to the original user, this function can execute a string as a javascript expression. Specifically, Eval accepts a string-type parameter,
Use this string as the code to execute in the context and return the execution result. 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 eval program, you will see that the string "2 + 3" in Javascript is actually executed. So when you
When the_evaled_answer is set 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. However, when using the eval function, pay attention to two points: 1) It has a return value. If the parameter string is an expression, the value of the expression is returned. If the parameter string is not an expression and has no value,
Then "undefined" is returned ". 2) When the parameter string is executed as a code, it is related to the context of calling the eval function, that is, the variable or function call must be
The context that calls Eval is available. 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, there is a function asking the user to change
Which image: Which image can be transformed? 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 = 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 = 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 ","");
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 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. Pai_^
  
Posted on Baldwin reading (2213) Comments (4) edit the category of the favorites: JavaScript, Web2.0 and CSS

This article is transferred from
Http://www.cnblogs.com/sunwangji/archive/2006/08/14/476233.html

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.