JS函數的參數(arguments)的使用


JS函數的參數在function內可以用arguments對象來獲取。

參數的調用有兩種方式:

1、期望參數的使用。

2、實際傳遞參數的使用。

應用舉例:

function Test(a, b){
var i, s = "Test函數有";
var numargs = arguments.length; // 獲取實際被傳遞參數的數值。
var expargs = Test.length; // 獲取期望參數的數值,函數定義時的預期參數個數(有a和b 2個參數)。
s += (expargs + "個參數。");

s += "\n\n"
for (i =0 ; i < numargs; i++){ // 獲取參數內容。
s += " 第" + i + "個參數是:" + arguments[i] + "\n";
}
return(s); // 返回參數列表。
}
alert(Test('param1','second param','第三個參數'));

需要注意的是:

arguments是一個object對象,它不是數組,不能對它使用shift、push、join等方法。

上述舉例時用的arguments[i]中的i只是作為arguments對象的屬性,並不能理解為數組下標。

代碼演示

 1 <html>
 2 <head>
 3    <script  language="javascript">
 4 
 5  function reloadList(){
 6 
 7   if(typeof arguments[0] == "function"){
 8       arguments[0].call(this);
 9     arguments[0]();
10     }
11 
12       if(typeof arguments[0] == "string")
13         alert(arguments[0]);
14 
15         if(typeof arguments[0] == "number")
16         alert(arguments[0]);
17 
18         if(typeof arguments[0] == "undefined")
19         alert(arguments[0]);
20 
21         if(typeof arguments[0] == "boolean")
22         alert(arguments[0]);
23 
24         if(typeof arguments[0] == "null")
25         alert(arguments[0]);
26 
27  }
28 
29 reloadList(function(){});
30 </script>
31 </head>
32 <body>
33 </body>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM