1.eval_r()
功能:eval_r(codeString)计算codeString(如果有的话)并返回,假如codeString是一段JavsScript代码,那么执行它。
eg1:
JavaScript语言:高亮代码由发芽网提供 01<script type="text/javascript">
02<!--
03eval("x=10;y=20;document.write(x*y)");
04document.write("<br
/>");
05
06document.write(eval("2+2"));
07document.write("<br
/>");
08var x=10;
09document.write(eval(x+17));
10-->
11</script>
12运行结果:
13200
144
1527
eg2:
如果试图覆盖 eval 属性或把 eval_r()
方法赋予另一个属性,并通过该属性调用它,则 ECMAScript 实现允许抛出一个 EvalError
异常JavaScript语言:1eval("2+3")
// 返回
5
2var myeval = eval;
// 可能会抛出 EvalError 异常
3myeval_r("2+3");
// 可能会抛出 EvalError 异常
可以使用下面这段代码来检测 eval_r() 的参数是否合法:JavaScript语言:1try {
2
alert("Result:" + eval(prompt("Enter
an expression:","")));
3
}
4
5catch(exception) {
6
alert(exception);
7
} 提示:虽然
eval_r() 的功能非常强大,但在实际使用中用到它的情况并不多。
2.isFinite()---->用于检查其参数是否是无穷大。
功能:isFinnite(number),如果 number 是有限数字(或可转换为有限数字),那么返回 true。否则,如果
number 是 NaN(非数字),或者是正、负无穷大的数,则返回 false.
eg1:
JavaScript语言:01<script type="text/javascript">
02<!--
03document.write(isFinite(123)+ "<br
/>")
04document.write(isFinite(-1.23)+ "<br
/>")
05document.write(isFinite(5-2)+ "<br
/>")
06document.write(isFinite(0)+ "<br
/>")
07document.write(isFinite("Hello")+ "<br
/>")
08document.write(isFinite("2005/12/12")+ "<br
/>")
09document.write(isFinite("1/3")+ "<br
/>")
10-->
11</script>
12运行结果:
13true
14true
15true
16true
17false
18false
19false 3.isNaN()---->用于检查其参数是否是非数字值(not a
number)
功能:isNaN(x),如果
x 是特殊的非数字值 NaN(或者能被转换为这样的值),返回的值就是 true。如果 x 是其他值,则返回
false.
eg1:
JavaScript语言:01<script>
02<!--
03document.write(isNaN(123));
04document.write(isNaN(-1.23));
05document.write(isNaN(5-2));
06document.write(isNaN(0));
07document.write(isNaN("Hello"));
08document.write(isNaN("2005/12/12"));
09document.write(isNaN("9-4+2"),"<br
/>")
10
-->
11</script>
12运行结果:
13true
14true
15true
16true
17false
18false
19false
必要性:NaN 与任何值(包括其自身)相比得到的结果均是
false,所以要判断某个值是否是 NaN,不能使用 == 或 === 运算符。正因为如此,isNaN()
函数是必需的。
提示:isNaN()
函数通常用于检测 parseFloat() 和
parseInt() 的结果,以判断它们表示的是否是合法的数字。当然也可以用 isNaN() 函数来检测算数错误,比如用 0
作除数的情况.
4.parseInt()和parseFloat()---->可解析一个字符串,并返回一个整数或浮点数
语法:parseInt(string,radix)
parseFloat(string,radix)
说明:
当参数radix 的值为
0,或没有设置该参数时,parseInt() 会根据string 来判断数字的基数。
举例,如果
string 以
"0x" 开头,parseInt() 会把string 的其余部分解析为十六进制的整数。如果 string 以
0 开头,那么parseInt()
的一个实现把其后的字符解析为八进制或十六进制的数字。如果string 以
1 ~ 9 的数字开头,parseInt() 将把它解析为十进制的整数
eg1:
JavaScript语言:01<script type="text/javascript">
02<!--
03document.write(parseInt("10") + "<br
/>")
04document.write(parseInt("19",10) + "<br
/>")
05document.write(parseInt("11",2) + "<br
/>")
06document.write(parseInt("17",8) + "<br
/>")
07document.write(parseInt("1f",16) + "<br
/>")
08document.write(parseInt("010") + "<br
/>")
09document.write(parseInt("He
was 40")+ "<br
/>")
10
-->
11</script>
12运行结果:
1310
1419
153
1615
1731
18NaN
提示:(1)、只有字符串中的第一个数字会被返回,(2)、开头和结尾允许空格
5.Number()和String()--->将对象转换为数字或字符串
eg1:Number(object)将对象转化称原始数字值
JavaScript语言:01<script type="text/javascript">
02<!--
03
04var test1= new Boolean(true);
05var test2= new Boolean(false);
06var test3= new Date();
07var test4= new String("999");
08var test5= new String("999
888");
09
10document.write(Number(test1)+ "<br
/>");
11document.write(Number(test2)+ "<br
/>");
12document.write(Number(test3)+ "<br
/>");
13document.write(Number(test4)+ "<br
/>");
14document.write(Number(test5)+ "<br
/>");
15
16
-->
17</script>
18运行结果:
191
200
211375813072017------》自1970年1月1日至今的毫秒数
22999
23NaN eg2:String(Object)将对象转换称原始字符串值
JavaScript语言:01<script type="text/javascript">
02<!--
03
04var test1= new Boolean(1);
05var test2= new Boolean(0);
06var test3= new Boolean(true);
07var test4= new Boolean(false);
08var test5= new Date();
09var test6= new String("999
888");
10var test7=12345;
11
12document.write(String(test1)+ "<br
/>");
13document.write(String(test2)+ "<br
/>");
14document.write(String(test3)+ "<br
/>");
15document.write(String(test4)+ "<br
/>");
16document.write(String(test5)+ "<br
/>");
17document.write(String(test6)+ "<br
/>");
18document.write(String(test7)+ "<br
/>");
19
-->
20</script>
21运行结果:
22true
23false
24true
25false
26Wed Aug 07 2013 02:20:50 GMT+0800 (CST)
27999 888
2812345
6.escape()
功能:
escapse(string),函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串
(string)可以
对 escape()
编码的字符串进行解码
eg1:
JavaScript语言:01<script type="text/javascript">
02<!--
03
04document.write(escape("Visit
W3School.com.cn!")+ "<br
/>")
05document.write(escape("?!=()#%&"),"<br
/>")
06document.write(("Visit
W3School.com.cn!"),"<br
/>")
07document.write(("Visit
W3School.com.cn!"),"<br
/>")
08
-->
09</script>
10运行结果:
11Visit%20W3School.com.cn%21
12%3F%21%3D%28%29%23%25%26
13Visit W3School.com.cn!
14Visit W3School.com.cn! 说明:该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些
ASCII 标点符号进行编码: * @ - _ + . / 。其他所有的字符都会被转义序列替换。