js 函數參數個數

來源:互聯網
上載者:User

標籤: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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.