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对象的属性,并不能理解为数组下标。

代码演示


<html>
<head>
   <script  language="javascript">

 function reloadList(){

  if(typeof arguments[0] == "function"){
  	arguments[0].call(this);
	arguments[0]();
	}

  	if(typeof arguments[0] == "string")
  	  alert(arguments[0]);

  	  if(typeof arguments[0] == "number")
        alert(arguments[0]);

		if(typeof arguments[0] == "undefined")
        alert(arguments[0]);

		if(typeof arguments[0] == "boolean")
        alert(arguments[0]);

		if(typeof arguments[0] == "null")
        alert(arguments[0]);

 }

reloadList(function(){});
</script>
</head>
<body>
</body>