js中的arguments

arguments:在函数调用中,会自动在该函数内部生成一个名为arguments的隐藏对象。该对象类似于数组,但又不是数组。可以使用[]操作符获取函数调用时传递的实参。

<script>

function test(){

  alert(arguments.length);

  for(var i=0;i<arguments.length;i++){

    alert(arguments[i]);

  }

}

test(1);//1

test('a','b');//2

</script>

注:arguments保存的是实参的信息。

用instanceof(Array)来检查,返回false,说明arguments不是数组。typeof(arguments)返回为object。

只有在函数被调用时,arguments对象才会创建,未调用时其值为null。

比如直接 alert(new Function().argument);//返回null

caller

在一个函数调用另一个函数时,被调用函数会自动生成一个caller属性,指向调用它的函数对象。如果该函数当前未被调用,或并非被其他函数调用,则caller为null。

function testCaller(){

  var caller=testCaller.caller;

  alert(caller);

}

function aCaller(){

  testCaller();

}

aCaller();

callee

当函数被调用时,它的arguments.callee对象就会指向自身,也就是一个对自己的引用。由于arguments在函数被调用时才有效,因此arguments.callee在函数未调用时是不存在的。

function aCallee(arg){

 alert(arguments.callee);

}

function aCaller(arg1,arg2){aCallee();}

aCaller();

使用callee在闭包中计算10的阶乘:

(function(x){return x>1:x*arguments.callee(x-1):1})(10);

原文地址:https://www.cnblogs.com/lionisnotkitty/p/6306078.html