eval () function

Definition and Usage

       eval () function computes a string, and executes the JavaScript code.

grammar

    eval(string)

return value

Calcd by (if any) string obtained.

Explanation

This method only accepts original string as a parameter, the parameter string is not if the original string, then the method will return without any change. So please do not eval () function is passed as a parameter to a String object.

If you try to cover the eval property or the eval () method gives another property, and call it the property, the ECMAScript implementation allowed to throw a EvalError exception.

Throw out

If the parameter is not legitimate expressions and statements, SyntaxError exception is thrown.

If the illegal call eval (), an exception is thrown EvalError.

If passed to the eval () Javascript code generates an exception, eval () of the anomaly will be transferred to the caller.

Example 1

In this example, we will use on several string eval (), and see the results returned:

<script type="text/javascript">
eval("x=10;y=20;document.write(x*y)")  //200
document.write(eval("2+2"))   //4
var x=10
document.write(eval(x+17))  //27
</script>

Example 2

Look at other cases, eval () returns the result of:

the eval ( "2 +. 3")     // returns. 5 
var myeval the eval =;     // may throw an exception EvalError 
myeval ( "2 +. 3");     // may throw an exception EvalError

You can use the following code to detect the eval () argument is legitimate:

try  {
     alert("Result:" + eval(prompt("Enter an expression:","")));
     }

catch(exception) {
     alert(exception);
     }

 

   

Guess you like

Origin www.cnblogs.com/zouhong/p/12049996.html