為什么說(shuō)arguments是偽(類(lèi))數(shù)組?
答:因?yàn)閍rguments它不是數(shù)組,卻用起來(lái)像數(shù)組,有l(wèi)ength屬性和[ ]訪問(wèn)成員。但是不具有數(shù)據(jù)的方法,如join()、concat()等。。。
怎樣將arguments轉(zhuǎn)換成數(shù)組
Array.prototype.slice.apply(arguments)

image.png
轉(zhuǎn)換前

image.png
轉(zhuǎn)換后

image.png
1、數(shù)組長(zhǎng)度
window.onload = function(){
function abc(){
console.log(arguments.length)
}
abc(1,2,3)
}// 3
2、改變參數(shù)值
window.onload = function(){
function abc(x,y,z){
arguments[2] = "hello";
for(var i=0;i<=arguments.length;i++){
console.log(" "+arguments[i]);
}
}
abc(1,2,3)
}// 1,2,hello
3、遞歸(callee()調(diào)用自身)
求1到n的自然數(shù)之和
function add(x){
if(x == 1) return 1;
else return x + arguments.callee(x-1);
}
console.log(add(5))//15
對(duì)于沒(méi)有命名的函數(shù)
var result = function(x){
if(x == 1) return 1;
return x+arguments.callee(x-1);
}
console.log(result(5))//15