anguments

anguments是一个对象,长得很像数组的对象,但不是数组,而是伪数组

arguments的内容是函数运行时的实参列表

(function(d, e, f) {
    console.log(arguments);       //["hello","world","!","aa"]
    console.log(arguments[3]);    //aa
})('hello', 'world', '!','aa');

传进匿名函数的参数只有前三个,实际上却能从arguments里获取到第四个。

 

----------------------------------------------------分割线----------------------------------------------------

arguments的特点:

1、传进函数的实参本来只能通过形参传递,但是js里可以通过arguments传递

2、如果实参个数比形参要多,arguments仍然可以获取到,并且可以修改形参的值,修改形参数值的时候,形参也是做相应变化的。变化是双向的。

3、有2个常用属性,length获取长度,callee获取当前所在函数

(function(d, e, f) {
    console.log(arguments); //["hello","world","!","aa"]
    arguments[0]='china';
    console.log(d);         //["chia","world","!","aa"]
    e='s';
    console.log(arguments);   //["china","s","!","aa"]
    var length=arguments.length;
    console.log(length);         //4
    console.log(arguments.callee);   //答案在下面 详见:答一
})('hello', 'world', '!','aa');

答一

 function (d, e, f) {
    console.log(arguments);
    arguments[0]='china';
    console.log(d);
        e='s';
        console.log(arguments);
    var length=arguments.length;
    console.log(length);
    console.log(arguments.callee);
}

----------------------------------------------------分割线----------------------------------------------------

使用callee计算1到n的和的函数

console.log((function(n){
    if(n==1){
        return 1;
    }else{
        return n+arguments.callee(n-1);      //10+9+8+7+6+5+4+2+1=55
    }
}(10)));
原文地址:https://www.cnblogs.com/coldfishdt/p/6532955.html