标签:char 函数重载 lib alt 查询 return 数值 var write
function Person(){ this.id = arguments[0]; //在函数中可以直接使用arguments变量,arguments是一个数组,表示所有传递过来的参数。 this.name = arguments[1]; }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>javascript函数</title></head><script type="text/javascript">/*javascript函数函数的定义格式:function 函数名(形参名列表){函数体;}javascript定义函数时需要注意的细节:1.在javascript定义函数的时候,形参不能使用var关键字声明变量类型,只写形参的名称就可以了.2.在javascript中的函数是没有返回值类型的,如果函数需要返回数据类型给调用者,直接返回即可,如果不需要返回则不返回。3.在javascript中没有函数重载的概念,后定义的同名函数会直接覆盖前面定义的同名函数。4.在javascript中任何的函数内部都隐式维护了一个arguments(数组)的对象,给函数传递数据的时候,是会先传递到arguments对象中,然后再由arguments对象分配数据给形参的。5.在函数中可以直接使用arguments变量,arguments是一个数组,表示所有传递过来的参数。6.在声明函数时,定义的形参就对应arguments数组中相应位置的参数值,写形参只是用着方便一点。但是不推荐,不方便分析函数*///需求:定义一个函数做两个参数的加法功能。with(document){function add1(a, b){var sum = a + b;// document.write(sum);return sum;}//调用函数var result = add1(1, 2);document.write(result);document.write("<hr/>");function add2(a, b){var sum = a + b;document.write(sum);}//调用函数var result = add2(1, 2);document.write("两个函数总和" + result);//undefined 函数中没有返回值时,return可以有,也可以没有。若有return返回值为undefineddocument.write("<hr/>");// javascript中的函数没有重载的形式,后定义的函数会直接覆盖前面 的函数。function add2(a, b, c){var sum = a + b + c;document.write("三个函数总和" + sum);}//调用函数add2(1, 2, 3);add2(1, 2);document.write("<hr/>");function add2(){document.write("arguments对象长度:" + arguments.length + "\t, ");var sum = 0;document.write("0个函数总和" + sum);for(var i=0; i<arguments.length; i++){//在函数中可以直接使用arguments变量,arguments是一个数组,表示所有传递过来的参数。document.write(arguments[i]);//输出arguments对象中的参数}}add2(1, 2, 4, 5, 6);write("<hr/>");//在声明函数时,定义的形参就对应arguments数组中相应位置的参数值,写形参只是用着方便一点。但是不推荐function Person(){this.id = arguments[0]; //在函数中可以直接使用arguments变量,arguments是一个数组,表示所有传递过来的参数。this.name = arguments[1];}var p = new Person(111, "木丁西");write(p.id + "\t" + p.name);}</script><body></body></html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>javascript函数练习</title><script type="text/javascript">/*需求:定义一个输入框输入的月份,然后输出本月对应的天数。31天:1,3,5,7,8,10,1230天:4,6,9,1128天:2*/function showDay(){//找到对应的标签对象var monthObj = document.getElementById("month");// alert(month.value);//访问对象的值 对象.value//获取到输入框中的月份值var month = monthObj.value;//判断输入框中的数据是不是数字if(isNaN(month)){alert("请输入月份(数字)。。。");return;}//将月份字符串转换成数字month = parseInt(month); //和java一样,switch case中的值必须要与switch表达式的值具有相同的数据类型。获取到对象的值为string,而case后面的是number,所以得统一数据类型。//判断输入框中的数据是否是月份if(month<1 || month>12){alert("请输入正确的月份(1-12)。。。");return;}switch(month){case 2:alert("28天");break;case 4:case 6:case 9:case 11:alert("30天");break;default:alert("31天");}}</script></head><body>月份<input type="text" id="month"/><input onclick="showDay();" type="button" value="查询" /></body></html>

标签:char 函数重载 lib alt 查询 return 数值 var write
原文地址:http://www.cnblogs.com/itMiracle/p/5990728.html