eval function and function isNaN

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

(B) Syntax: eval (string) string Required.

(C) Return value: value (if any) obtained by calculating the string

(Iv) Description:

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.

(E) Throws:

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.

(Vi) Examples:

1 <script type="text/javascript">
2 eval("x=10;y=20;document.write(x*y)")
3 //结果200
4 document.write(eval("2+2"))
5 //结果4
6 var x=10
7 document.write(eval(x+17))
8 //结果27
9 </script>

The exception instance:

// the eval () returns the result: 
// the eval ( "2 +. 3") // returns. 5 
// var = myeval the eval; EvalError exception may be thrown // 
// myeval ( "2 +. 3"); / / EvalError may throw an exception 
// You can use the following code to detect the eval () argument is legitimate: 
the try   {
 the try   { 
     Alert ( "the Result:" + eval (prompt ( "the Enter expression the AN:", "" ) )); 
     } 

the catch (Exception) { 
     Alert (Exception); 
     }

 

isNaN function:

(A) Definitions and Usage: () function is used to check whether the non-numeric parameters isNaN.

(B) Syntax: isNaN (x) x Required. Value to be detected.

(C) Return Value: If x is a special non-numeric values NaN (or can be converted to such a value), the return value is true. If x is another value, false is returned.

(Iv) Description: isNaN () function can be used to judge whether its argument is a NaN, the value indicates an invalid number (such as the results obtained after division by zero).

If the result NaN with any value (including itself) are obtained compared to false, so to determine whether a value is NaN, or can not use == === operator. Because of this, isNaN () function is required.

tip: isNaN () function is commonly used to detect the result parseFloat () and the parseInt () to judge whether or not they represent a legitimate number. Of course, also be used isNaN () function to detect an error count, such as the case with 0 divisor.

(E) examples:

 1 <script>
 2 document.write(isNaN(123));
 3 //false
 4 document.write(isNaN(-1.23));
 5 //false
 6 document.write(isNaN(5-2));
 7 //false
 8 document.write(isNaN(0));
 9 //false
10 document.write(isNaN("Hello"));
11 true
12 document.write(isNaN("2005/12/12"));
13 true
14 </script>

 

Guess you like

Origin www.cnblogs.com/xiaochen-cmd-97/p/11298198.html