標籤:java 使用 io for re c cti html
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>