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屬性
*/