0001. JavaScript Function.arguments 属性详解
发布于 2022年 02月 16日 22:21
arguments
属性是正在执行的函数的内置属性,返回该函数的arguments
对象。arguments
对象包含了调用该函数时所传入的实际参数信息(参数个数、参数值等)
该属性属于
Function
对象,所有主流浏览器均支持该属性
语法
[functionObject.]arguments
只有在当前函数
正在执行时
该属性才有效
返回值
arguments
属性的值为Object
类型,返回正在执行的当前函数的arguments
对象
arguments
对象包含调用该函数时所传入的实际参数信息,例如:参数的个数和参数的值。我们可以通过arguments
属性让函数处理可变数量的参数
arguments
对象有以下三个属性:
- length属性,返回实际传入的参数个数。
- callee属性,返回当前函数的引用(匿名函数可以使用该属性实现递归调用)。
- 0...n属性,以顺序索引访问传入的具体参数。例如,使
用arguments[0]
可以访问传入的第1个参数,arguments[1]
可以访问传入的第2个参数
arguments
对象具有length
属性和0...n
属性,看起来与数组的访问方式相同,但arguments
并不是数组,它没有数组对象所具备的其他成员属性和方法。
示例&说明
function test() {
// 实际传入的参数个数:3
document.writeln('实际传入的参数个数:' + arguments.length);
/* "test."可以省略 */
for(var i = 0; i < test.arguments.length; i++){
document.writeln('传入的第' + (i + 1) + '个参数:'+ arguments[i]);
}
// 传入的第1个参数:1 传入的第2个参数:张三 传入的第3个参数:true
// callee属性返回的就是当前函数
document.writeln( arguments.callee === test ); // true
};
test(1, '张三', true);
length属性详解
arguments.length
属性用于返回调用当前函数所传入的真实参数个数。
该属性属于arguments
对象,所有主流浏览器均支持该属性
语法
[functionObject.]arguments.length
返回值
arguments.length
属性的值为Number
类型,返回调用当前函数所传入的实际参数个数。
arguments.length
属性只有当函数开始执行时才被定义
示例&说明
function test() {
document.writeln('实际传入参数个数为: ' + arguments.length);
};
test(); // 实际传入参数个数为: 0
function foo(a, b) {
// "foo."可以省略
document.writeln('实际传入参数个数为: ' + foo.arguments.length);
};
foo(1, 2); // 实际传入参数个数为: 2
function bar(a, b) {
document.writeln('实际传入参数个数为: ' + arguments.length);
}
bar(1, 2, 3, 4); // 实际传入参数个数为4
callee属性详解
arguments.callee
属性用于返回当前正在执行的Function
对象
语法
[functionObject.]arguments.callee
返回值
arguments.callee
属性的值为Function
类型,返回当前正在执行的函数
简而言之,arguments.callee
返回该 arguments
对象所属的函数
arguments.callee
属性是arguments
对象的一个成员,该属性仅当相关函数正在执行时才可用
arguments.callee
属性的初始值是正被执行的Function
对象。这将允许匿名函数成为递归的
示例&说明
function test() {
// "test."可以省略
document.writeln( test.arguments.callee );
// function test(){ document.writeln( arguments.callee ); document.writeln( arguments.callee === test ); }
// arguments.callee 就是当前执行函数的自身
document.writeln( arguments.callee === test ); // true
};
test();
// 输出斐波那契数列(1,2,3,5,8,13……)第10项的数字
// 内部使用匿名函数+函数递归来实现
// 由于是匿名函数,在函数内部无法通过函数名来递归调用该函数
// 我们可以通过arguments.callee来取得当前匿名函数,从而进行递归调用
document.writeln(function(n) {
if (n <= 2) {
return n;
} else {
return arguments.callee(n - 1) + arguments.callee(n - 2);
}
}(10));
n个参数属性详解
arguments
对象的0...n
属性用于返回调用当前函数所传入的参数。访问arguments
的这些属性类似于访问数组中的元素
该属性属于
arguments
对象,每个函数在被调用时,都有一个内置的arguments
对象。所有主流浏览器均支持该属性
语法
[functionObject.]arguments[ n ]
参数
参数 | 描述 |
---|---|
n | Number 类型传入参数的顺序索引,第一个参数的索引为0 |
参数n应该是在
0
到arguments.length-1
范围内的整数。其中,0
表示传入的第一个参数,1
表示传入的第二个参数……arguments.length-1
表示传入的最后一个参数。如果参数n
超出该范围,将返回undefined
返回值
0...n
的值为任意类型,返回调用当前函数所传入的第n + 1
个参数
由
0...n
属性返回的值就是传递给正在执行的当前函数的参数值。虽然arguments
对象不是数组,但访问组成arguments
对象的各个元素的方法与访问数组元素的方法相同
示例&说明
function test() {
// 循环输出调用当前函数时传入的参数
for(var n = 0; n < arguments.length; n++){
document.write( arguments[n] + '|');
}
document.write('<br>'); // 输出换行符进行区分
};
test(); // (空字符串)
test('Code', 'huarxia.love', 12, true); // Code|huarxia.love|12|true|
test('张三', false, 12.315); // 张三|false|12.315|
function foo() {
// arguments前也可以加上函数名称
for(var n = 0; n < foo.arguments.length; n++){
document.write( foo.arguments[n] + '|');
}
document.write('<br>');
};
foo('Hello', 'World'); // Hello|World|
// 使用arguments对象实现任意多个数的求和
function sum() {
var sum = arguments[0];
for(var n = 1; n < arguments.length; n++){
sum += arguments[n];
}
return sum;
}
document.writeln( sum() ); // undefined
document.writeln( sum( 1, 5 ) ); // 6
document.writeln( sum( 1, 212, 21, -14, 23 ) ); // 243