JavaScript中的预定义断言函数

1.eval_r()
功能:eval_r(codeString)计算codeString(如果有的话)并返回,假如codeString是一段JavsScript代码,那么执行它。
eg1:
JavaScript语言 高亮代码由发芽网提供
01 <script  type= "text/javascript">
02  <!--
03  eval( "x=10;y=20;document.write(x*y)");
04  document.write( "<br />");
05 
06  document.write( eval( "2+2"));
07  document.write( "<br />");
08  var  x= 10;
09  document.write( eval(x+ 17));
10 -->
11 </script>
12 运行结果:
13  200
14  4
15  27
eg2:
如果试图覆盖 eval 属性或把 eval_r() 方法赋予另一个属性,并通过该属性调用它,则 ECMAScript 实现允许抛出一个 EvalError 异常
JavaScript语言:
eval( "2+3")     // 返回 5
var  myeval  eval;     // 可能会抛出 EvalError 异常
myeval_r( "2+3");     // 可能会抛出 EvalError 异常
可以使用下面这段代码来检测 eval_r() 的参数是否合法:
JavaScript语言:
try    {
        alert( "Result:"  eval(prompt( "Enter an expression:", "")));
        }

catch(exception)  {
        alert(exception);
        }
提示虽然 eval_r() 的功能非常强大,但在实际使用中用到它的情况并不多。
2.isFinite() ----> 用于检查其参数是否是无穷大。
功能:isFinnite(number), 如果 number 是有限数字(或可转换为有限数字),那么返回 true。否则,如果 number 是 NaN(非数字),或者是正、负无穷大的数,则返回 false.
eg1:
JavaScript语言:
01 <script  type= "text/javascript">
02  <!--
03  document.write( isFinite( 123)+  "<br />")
04  document.write( isFinite(- 1.23)+  "<br />")
05  document.write( isFinite( 5- 2)+  "<br />")
06  document.write( isFinite( 0)+  "<br />")
07  document.write( isFinite( "Hello")+  "<br />")
08  document.write( isFinite( "2005/12/12")+  "<br />")
09  document.write( isFinite( "1/3")+  "<br />")
10     -->
11 </script>
12 运行结果:
13  true
14  true
15  true
16  true
17  false
18  false
19  false
3.isNaN()- ---> 用于检查其参数是否是非数字值(not a number)
功能:isNaN(x), 如果 x 是特殊的非数字值 NaN(或者能被转换为这样的值),返回的值就是 true。如果 x 是其他值,则返回 false.
eg1:
JavaScript语言
01 <script>
02  <!--
03  document.write( isNaN( 123));
04  document.write( isNaN(- 1.23));
05  document.write( isNaN( 5- 2));
06  document.write( isNaN( 0));
07  document.write( isNaN( "Hello"));
08  document.write( isNaN( "2005/12/12"));
09  document.write( isNaN( "9-4+2"), "<br />")
10       -->
11 </script>
12 运行结果:
13  true
14  true
15  true
16  true
17  false
18  false
19  false
必要性: 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  <!--
03  document.write( parseInt( "10" "<br />"
04  document.write( parseInt( "19", 10 "<br />"
05  document.write( parseInt( "11", 2 "<br />"
06  document.write( parseInt( "17", 8 "<br />"
07  document.write( parseInt( "1f", 16 "<br />"
08  document.write( parseInt( "010" "<br />")
09  document.write( parseInt( "He was 40" "<br />")
10       -->
11 </script>
12 运行结果:
13  10
14  19
15  3
16  15
17  31
18  NaN

提示:(1)、只有字符串中的第一个数字会被返回,(2)、开头和结尾允许空格

5.Number()和String()--->将对象转换为数字或字符串

eg1:Number(object)将对象转化称原始数字值

JavaScript语言
01 <script  type= "text/javascript">
02  <!--
03 
04  var  test1=  new  Boolean( true);
05  var  test2=  new  Boolean( false);
06  var  test3=  new  Date();
07  var  test4=  new  String( "999");
08  var  test5=  new  String( "999 888");
09 
10  document.write( Number(test1)+  "<br />");
11  document.write( Number(test2)+  "<br />");
12  document.write( Number(test3)+  "<br />");
13  document.write( Number(test4)+  "<br />");
14  document.write( Number(test5)+  "<br />");
15 
16       -->
17 </script>
18 运行结果:
19  1
20  0
21  1375813072017------》自1970年1月1日至今的毫秒数
22  999
23  NaN
eg2:String(Object)将对象转换称原始字符串值
JavaScript语言
01 <script  type= "text/javascript">
02  <!--
03 
04  var  test1=  new  Boolean( 1);
05  var  test2=  new  Boolean( 0);
06  var  test3=  new  Boolean( true);
07  var  test4=  new  Boolean( false);
08  var  test5=  new  Date();
09  var  test6=  new  String( "999 888");
10  var  test7= 12345;
11 
12  document.write( String(test1)+  "<br />");
13  document.write( String(test2)+  "<br />");
14  document.write( String(test3)+  "<br />");
15  document.write( String(test4)+  "<br />");
16  document.write( String(test5)+  "<br />");
17  document.write( String(test6)+  "<br />");
18  document.write( String(test7)+  "<br />");
19       -->
20 </script>
21 运行结果:
22  true
23  false
24  true
25  false
26 Wed  Aug  07  2013  02: 20: 50  GMT+ 0800  (CST)
27  999  888
28  12345
6.escape() 
功能: escapse(string),函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串
(string) 可以 对 escape() 编码的字符串进行解码
eg1:
JavaScript语言
01 <script  type= "text/javascript">
02  <!--
03 
04  document.write(escape( "Visit W3School.com.cn!" "<br />")
05  document.write(escape( "?!=()#%&"), "<br />")
06  document.write(( "Visit W3School.com.cn!"), "<br />")
07  document.write(( "Visit W3School.com.cn!"), "<br />")
08       -->
09 </script>
10 运行结果:
11 Visit% 20W3School.com.cn% 21
12 % 3F% 21% 3D% 28% 29% 23% 25% 26
13 Visit  W3School.com.cn!
14 Visit  W3School.com.cn!
说明: 该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / 。其他所有的字符都会被转义序列替换。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值