<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script>
//函数默认有一个隐式的arguments属性,代表实际传递的参数
/** function test1(no1,no2){
alert(no1);//显示1
alert(no2);//显示3
alert(arguments[0]);//显示1,代表传递的第一个实际参数
alert(arguments[1]);//显示3,代表传递的第二个实际参数
alert(arguments[2]);//显示undefined,显然未传递第三个参数显示undefined
}
test1(1,3);//这里传进两个参数不穿第三个参数
**/
/*
//这里函数定义两个参数,在test2(1,3,4)中我传递三个进去
function test2(no1,no2){
alert(no1);//显示1
alert(no2);//显示3
alert(arguments[0]);//显示1,代表传递的第一个实际参数
alert(arguments[1]);//显示3,代表传递的第二个实际参数
alert(arguments[2]);//显示4, 代表传递的第三个实际参数
//要注意实际传递一词
alert(arguments.length);//显示3,代表实际传递的个数,这里传递了三个参数进来所以就是3个
}
test2(1,3,4);//显然函数体只定义了两个参数,但是我可以传递三个参数进去
*/
//所以用arguments 就可以模拟数函数的重载
function add(){
if(arguments.length == 1){
alert(arguments[0]);
}else if(arguments.length == 2){
alert(arguments[0]+arguments[1]);
}else if(arguments.length == 3){
alert(arguments[0] + arguments[1] + arguments[2]);
}
}
add(1);//显示1
add(1,2);//显示2
add(1,2,3);//显示3
//总结 在javascript 中每个函数都有一个隐含的对象arguments,表示给函数实际传递的参数。arguments.length标示实际给函数传递参数的个数
</script>
</head>
<body>
</body>
</html>
javascript_function方法中的arguments
最新推荐文章于 2022-12-20 21:01:26 发布