js全局函数以及重载

来源:互联网 发布:超星网络教学平台 编辑:程序博客网 时间:2024/06/06 10:51

js的全局函数

由于不属于任何一个对象,直接写名成使用

eval(); 执行JS代码

encodeURI:对字符进行编码

decodeURI:对字符进行解码

isNaN():  判断当前是否是数字,返回值true false,是数字返回false,不是数字返回true;

parseInt() 类型转换

<body><script type="text/javascript">var str="alert('123');";alert(str);eval(str);//encodeURLdocument.write("<hr/>");var str1="测试中文aaa234";var encode1=encodeURI(str1);document.write(encode1);document.write("<hr/>");var decode1=decodeURI(encode1);document.write(decode1);document.write("<hr/>");var str2="123";alert(isNaN(str2));document.write("<hr/>");var str3="223";document.write(parseInt(str3)+1);</script></body>
js函数的重载

重载:方法名相同参数不同

js的重载是否存在?不存在

调用最后一个方法,把传递的参数保存到一个数组里面arguments数组

1.js里面不存在重载 2.但是可以通过其他方来模拟重载

<body><script type="text/javascript">/*function add1(a,b){//alert(arguments.length);for(var i=0;i<arguments.length;i++){alert(arguments(i));}return a+b;}*//*function add1(a,b,c){return a+b+c;}*/function add1(){//比如传递的是两个参数if(arguments.length==2){return arguments[0]+arguments[1];}else if(arguments.length==3){return arguments[0]+arguments[1]+arguments[2];}else{return 0;}}alert(add1(1,2));alert(add1(1,2,3));alert(add1(1,2,3,4));</script></body>