javascript中的arguments是一个长得很像数组的对象,然而它不是数组。arguments内容是函数运行时的实参列表,它收集了所有的实参。若想知道实参个数可以通过arguments.length得到。形参与对应的arguments单元是相互映射,相互影响的。其中arguments.callee代表当前运行的函数。下面看几个例子:
eg1:
(function(a,b,c){
console.log(arguments[0]);//hello
})('hello');
eg2:
(function(a,b,c){
console.log(arguments[3]);//haha
})('hello','world','!','haha');
/*
实参长度比形参长依然可以通过arguments得到在形参里面没有声明的实参
*/
eg3:
(function(a,b,c){
arguments[0] = 'world';
console.log(arguments[0]);//world
})('hello');
/*
形参与对应的arguments单元是相互映射,相互影响的
*/
eg4:
(function(a,b,c){
console.log(arguments.callee);
/*
function (a,b,c){
console.log(arguments.callee);//
}
*/
})('hello');
/*
arguments.callee代表当前函数
*/
function t(n){
if(n<=1){
return 1;
}else{
return n + t(n-1);
}
}
alert(t(10));//55
alert((function(n){
if(n<=1){
return 1;
}else{
return n + arguments.callee(n-1);
}
})(10));
/*
要在匿名函数上完成递归就需要arguments.callee属性
*/