1.arguments表示传参列表,它和实参列表相对应。
function test(x){
console.log(arguments); //arguments=[1,2,3]
}
test(1,2,3)
注:arguments和形参是相映射的关系
function test(x){
x = 2;
console.log(arguments); //argumnets=[2,2,3]
}
test(1,2,3)
如果修改x的值,arguments的值也会发生改变,同理,如果改变argumnets的值,x的值也会发生改变。
2.arguments是一个类数组
3.arguments.callee
function test(){
console.log(arguments.callee);
}
test();
arguments.callee的返回结果就是函数引用,即function test(){}
那么arguments.callee有什么作用呢?
例如我们可以利用arguments.callee拿到一个匿名函数的引用
例子:求100的阶乘
var result = (function(n){
if(n ==1) return 1;
return n * arguments.callee(n-1);
}(100))
这是一种用递归的方法实现阶乘的方法,因为是用的立即执行函数,并不能拿到函数名称,所以可以通过arguments.callee来解决这个问题。
还有类似这样,用setTimeOut实现倒计时
(function (n){
if(n==0) return;
var args = arguments;
console.log(n);
setTimeout(function(){
n--;
arguments.callee(n);
},1000);
})()
还有一个关于function的用法,function.caller
function test(){
demo()
}
function demo(){
console.log(demo.caller) //function test(){}
}
test()
function.caller返回的是该函数被调用的环境