How to use the Eval function in JavaScript and examples _ basics

Source: Internet
Author: User
Tags eval object model

Definition and usage the eval () function computes a string and executes the JavaScript code in it.

The syntax eval (string) argument describes string as required. The string to evaluate, which contains the JAVASCRIPT expression to evaluate or the statement to execute. The return value is computed by calculating the value of string (if any).

Indicates that the method accepts only the original string as a parameter, and if the string argument is not the original string, the method returns without any changes. Therefore, do not pass a String object as an argument for the eval () function.

If you attempt to overwrite the Eval attribute or assign the eval () method to another property and call it through this property, the ECMAScript implementation allows a Evalerror exception to be thrown.

Throws an SyntaxError exception if there are no valid expressions and statements in the argument.

If Eval () is invoked illegally, a Evalerror exception is thrown.

If the Javascript code passed to eval () generates an exception, eval () passes the exception to the caller.

Hint and comment tip: Although the eval () is very powerful, it is not much used in practice.

Here are a few common examples for you to share:

<script language= "javascript" >
function showsubmenu (SID)
{
Whichel = eval ("submenu" + SID);
if (WhichEl.style.display = = "None")
{
eval ("submenu + Sid +". style.display=\ "\;");
}
else
{
eval ("submenu + Sid +". Style.display=\ "none\");
}
</SCRIPT>

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);

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 the program itself based on time or user input, by taking a counter

Three, you can get amazing results.

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:

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:

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.

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.