JavaScript学习日志——内建的全局函数

来源:互联网 发布:数控加工编程代码 编辑:程序博客网 时间:2024/06/09 14:34

JavaScript学习日志——流程控制


JavaScript中内建了很多预定义的函数(全局函数)用于处理一些常见的操作。


1. eval()函数

将表达式转换为变量名或对象

document.write("eval()函数:");var x3="hello world!";var x=3;var y=eval("x"+x);document.write(y+"<br>"+"<br>");//将显示x3变量的内容:"hello world!"

2. 转义操作函数

2.1 escape() 和 unescape()函数

  1. escape()用于将参数转换为字符串,并以URL编码格式进行编码。
  2. unescape()是反向转义函数,功能与escape()相反。
//escape()将非ASCII字符用16进制编码代替//unescape()将上述内容解码返回//不能对Unicode字符编解码,不建议使用document.write("escape()与unescape():"+"<br>");var es=escape(x3);document.write("编码:"+es+"<br>");document.write("解码:"+unescape(es)+"<br>"+"<br>");

这两个函数在处理非ASCII字符时会出错,不能对Unicode字符进行编解码,故不推荐使用其进行转义和反向转义。


2.2 encodeURI() 和 decodeURI()函数

encodeURI()函数用于将字符编码为一个有效的统一资源标识符(URI)

//encodeURI()与decodeURI()//统一资源标识符URIdocument.write("encodeURI()与decodeURI():"+"<br>");var en=encodeURI(x3);document.write("编码:"+en+"<br>");document.write("解码:"+decodeURI(en)+"<br>"+"<br>");

因为其不能对&、+、=等字符进行编码,故单靠encodeURI()自身不能形成HTTP GET请求或POST请求。


2.3 encodeURIComponent() 和 decodeURIComponent()函数

encodeURIComponent()可以将用户输入的数据作为请求参数向服务器发送请求。

//encodeURIComponent()与decodeURIComponent()//用于向服务器发送数据document.write("encodeURIComponent()与decodeURIComponent():"+"<br>");var ec=encodeURIComponent(x3);document.write("编码:"+ec+"<br>");document.write("解码:"+decodeURIComponent(ec)+"<br>"+"<br>");

3. 转换函数

3.1 parseFloat()函数

将字符串转换为浮点数。此函数解析并返回字符串中的数字,直到到达不是数字部分的字符。如果一开始便不可分析,则返回NaN。

//parseFloat()document.write("parseFloat():"+"<br>");document.write("123 ----> "+parseFloat("123")+"<br>");document.write("123.123 ----> "+parseFloat("123.123")+"<br>");document.write("12.5e6 ----> "+parseFloat("12.5e6")+"<br>");document.write("abc ----> "+parseFloat("abc")+"    不是以可分析的数字字符开始的,则返回NaN<br>");document.write("123abc ----> "+parseFloat("123abc")+"    解析字符串中的数字字符<br>");document.write("0xyz ----> "+parseFloat("0xyz")+"    同上<br><br>");

3.2 parseInt()函数

语法格式:parseInt(expression,[radix])

  • expression为要转换为整数的字符串
  • radix为数字的进制
//parseInt()document.write("parseInt():"+"<br>");document.write("3.5 ----> "+parseInt("3.5")+"<br>");document.write("abc ----> "+parseInt("abc")+"<br>");document.write("3.5abc ----> "+parseInt("3.5abc")+"<br>");document.write("解析16进制 parseInt('0x3F8',16) ----> "+parseInt("0x3F8",16)+"<br>");document.write("解析2进制 parseInt('1010',2) ----> "+parseInt("1010",2)+"<br>");document.write("解析2进制 parseInt('777',8) ----> "+parseInt("777",8)+"<br><br>");

3.3 Number() 和String() 函数

将对象转换为数字或字符串

//Number()与String()document.write("Number()与String():"+"<br>");var D=new Date();document.write("Date类型 ----> "+String(D)+"<br>");var N=new Number(100);document.write("Number类型 ----> "+String(N)+"<br>"+"<br>");

3.4 Boolean() 函数

将一个对象转换为逻辑值

//Boolean()document.write("Boolean():"+"<br>");document.write("真值输入:"+"<br>");document.write("非零数字 Boolean(4) ----> "+Boolean(4)+"<br>");document.write("非空字符串 Boolean('abc') ----> "+Boolean("abc")+"<br>");document.write("Object类实例 Boolean(new Object()) ----> "+Boolean(new Object())+"<br><br>");document.write("假值输入:"+"<br>");document.write("数字0 Boolean(0) ----> "+Boolean(0)+"<br>");document.write("NaN Boolean(NaN) ----> "+Boolean(NaN)+"<br>");document.write("空字符串 Boolean('') ----> "+Boolean("")+"<br>");document.write("null Boolean(null) ----> "+Boolean(null)+"<br>");document.write("undefined Boolean(undefined) ----> "+Boolean(undefined)+"<br>");document.write("无参数 Boolean() ----> "+Boolean()+"<br><br>");

4 判断函数

4.1 isFinite() 函数

用于判断某个数值是否为有限数字,如为有限数字,返回true;反之返回false。

//判断函数document.write("判断是否为有限数字或可转换为有限数字 isFinite():"+"<br>");document.write("56 ----> "+Boolean(isFinite(56))+"<br>");

4.2 isNaN() 函数

用于判断某个数值是否是数字,如果不是数字,返回true。

//document.write("NaN(非数字),或者是正、负无穷大的数  '123abc' ----> "+Boolean(isFinite("123")+"<br>");document.write("判断是否不是数字 isNaN():"+"<br>");document.write("123 ----> "+isNaN(123)+"<br>");document.write("abc ----> "+isNaN("abc")+"<br>"+"<br>");
0 0
原创粉丝点击