天天看点

js函数的内部属性---arguments,callee,caller

在接下来的几篇文章中,我大家谈谈函数的内部属性,arguments,callee,caller

(1)arguments,是一个类数组对象,其中包含了传入函数的所有参数,主要用途是,保存函数的参数;

代码1:

function aa(b){alert(arguments);}

aa(4);

js函数的内部属性---arguments,callee,caller

function aa(a,b,c,d){alert(arguments.length);}

aa(1,2,3,4);

js函数的内部属性---arguments,callee,caller

function aa(a,b,c,d){alert(arguments[2]);}

js函数的内部属性---arguments,callee,caller

function Hi() {

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

alert("Hi, " + arguments[i])

}

 }

 Hi("Cat", "Alice");

js函数的内部属性---arguments,callee,caller
js函数的内部属性---arguments,callee,caller

(2)另外一个属性callee,该属性是一个指针,指向拥有这个arguments对象的函数。

(3)另一个属性calleer,该属性指向调用当前函数的函数的引用。

Arguments.callee典型应用:

var i = 1 setTimeout( function() {

 alert(i)if (i++<3) setTimeout(arguments.callee, 1000)

 }, 1000);

代码2:(递归时,实现代码解耦)

function test1(num){

 if(num<=1){

 return 1

}else

 return num*arguments.callee(num-1);

test1(5);//120

Arguments.callee.caller

Arguments.callee.caller 例子:

f();

function f() {

alert(arguments.callee.caller); // undefined

g();

 functiong() {

alert(arguments.callee.caller) ;

js函数的内部属性---arguments,callee,caller
js函数的内部属性---arguments,callee,caller

这一章简单介绍到这,后续几篇文章,将介绍函数中最重要的属性(没有之一),prototype和很牛逼的两个方法,apply,call();水平有限,请多多指教。