js中的函数参数个数不是确定区别函数的,也就是说你定义一个函数A(x,y) 当你使用这个函数时,你可以A();A(1);A(1,2);A(1,2,3);都是调用这个A(x,y)函数。
例子:
<html>
<head>
<title>
函数调用测试,参数个数和函数声明不一样多
</title>
<script language="JavaScript">
function needTwoPara(p1,p2){
var a=arguments;
var result=\'我是个需要2个参数的函数\n\'
+\'您输入的参数的个数为:\'+a.length+\'\n\'
for(var i=0, len = a.length; i < len; i++){
result=result+\'第\'+(i+1)+\'个参数为:\'+a[i]+\'\n\'
}
result+=\'以上是用arguments来获得参数\n\';
result+=\'下面用变量来获得参数:\n\';
result+=\'p1:\'+p1+\'\n\';
result+=\'p2:\'+p2+\'\n\';
alert(result);
}
</script>
</head>
<body>
<form>
<input type="button" value="测试1--传递1个参数"
onClick="JavaScript: needTwoPara(\'zhouli\');">
</form>
<form>
<input type="button" value="测试2--传递2个参数"
onClick="JavaScript: needTwoPara(\'zhouli\',\'周立\');">
</form>
<form>
<input type="button" value="测试3--传递3个参数"
onClick="JavaScript: needTwoPara(\'zhouli\',\'周立\',\'Journey\');">
</form>
<form>
<input type="button" value="测试4--传递4个参数"
onClick="JavaScript: needTwoPara(\'zhouli\',\'周立\',\'Journey\',\'Linkage_aya\');">
</form>
</body>
</html>