天天看點

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();水準有限,請多多指教。