Definition and usage
The Eval_r () function computes a string and executes the JavaScript code in it.
Grammar
Eval_r (String)
Parameters |
Describe |
String |
Necessary. The string to evaluate that contains the JAVASCRIPT expression to be evaluated or the statement to execute. |
return value
the value (if any) that is obtained by calculating the string.
Description
The method only accepts the original string as an argument, and if the string parameter is not the original string, the method returns without any change. Therefore, do not pass a String object as a parameter for the Eval_r () function.
If you attempt to override the eval property or assign the Eval_r () method to another property and call it through the property, the ECMAScript implementation allows a Evalerror exception to be thrown .
Thrown
Throws an SyntaxError exception if there are no valid expressions and statements in the argument .
If Eval_r () is called illegally, a Evalerror exception is thrown.
If the Javascript code passed to Eval_r () generates an exception,Eval_r () passes the exception to the caller.
Hints and Notes
Hint: Although eval_r () is very powerful, it is not used in many cases.
eg
<script language= "JavaScript" >
function Showsubmenu (SID)
{
Whichel = Eval_r ("submenu" + SID);
if (WhichEl.style.display = = "None")
{
Eval_r ("submenu" + Sid + ". style.display=\" \ ";");
}
Else
{
Eval_r ("submenu" + Sid + ". style.display=\" none\ ";");
}
}
</SCRIPT>
Eval_r () function
JavaScript has a number of tips to make programming easier.
One of them isThe Eval_r () function, which can treat a string as aJavaScript expression to execute it.
For a small example:
Executing an expression
var the_unevaled_answer = "2 + 3";
var the_evaled_answer = Eval_r ("2 + 3");
Alert ("The un-evaled answer is" + The_unevaled_answer + "and the evaled answer are" + the_evaled_answer);
If you run this sectionEval procedure, you will see theString in JavaScript"2 + 3" was actually executed.
So when you putThe value of the The_evaled_answer is set toEval_r ("2 + 3"), JavaScript will understand and put2 and3 of and returned toThe_evaled_answer.
This seems a bit silly, actually can make very interesting things. such as usingEval you can create functions directly based on 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 rarely used, but maybe you've seen someone useEval to get objects that are difficult to index.
Document Object Model (DOM) is one of the problems: Sometimes it's painful to get the object you're asking for.
For example, here's 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";
}
Together with theseImage tag:
[img src= "http://a.com/a.gif" Name= "Parrot"]
[img src= "http://b.com/b.gif" name= "cheese"]
[Code]
Note the following lines of statements like this:
[Code]
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 when you have100 instead of two images? You had to write a whole bunch of them.If-then-else statement, if it can be like this:
function Swaptwo ()
{
var the_image = prompt ("Change parrot or Cheese", "");
WINDOW.DOCUMENT.THE_IMAGE.SRC = "Ant.gif";
}
Unfortunately,, JavaScript will look for a name calledThe_image, not what you want."Cheese" orThe image of "parrot",
So you get the error message: "Never heard of a nameThe_image object ".
OKEval 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_r (the_image_name);
THE_IMAGE_OBJECT.SRC = "Ant.gif";
}
If the user fills in the prompt box"Parrot", in the second row, creates a string thatWindow.document.parrot. then contains theEval of the third
The line means:"Give me the objectWindow.document.parrot "-That's the image you want. Once you have acquired this image object, you can
It'sThe SRC attribute is set toAnt.gif. A little scared? Don't. In fact, this is very useful, and people often use it.
We are oftenjavascript Middle to eval this function,
Some people think this function is very strange, can make some strings become very powerful
This function is used when we need to turn ordinary strings into concrete objects The
eval function evaluates a string as a numeric expression with the following syntax:
eval_r (expr)
here expr is a string parameter that is evaluated. If the string is an expression, eval the value of the expression, if the parameter represents one or more javascript statement, then eval Execute these statements. eval function can be used to convert a date from a format (always a string) to a numeric expression or a number
JavaScript Eval_r () function