標籤:js函數中argument caller callee
在接下來的幾篇文章中,我大家談談函數的內部屬性,arguments,callee,caller
(1)arguments,是一個類數組對象,其中包含了傳入函數的所有參數,主要用途是,儲存函數的參數;
代碼1:
function aa(b){alert(arguments);}
aa(4);
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/2F/4E/wKioL1OfCNywbl45AABk9A88r80285.jpg" title="2.jpg" alt="wKioL1OfCNywbl45AABk9A88r80285.jpg" />
function aa(a,b,c,d){alert(arguments.length);}
aa(1,2,3,4);
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/2F/4E/wKioL1OfCTmhC1iqAABk9A88r80587.jpg" title="2.jpg" alt="wKioL1OfCTmhC1iqAABk9A88r80587.jpg" />
function aa(a,b,c,d){alert(arguments[2]);}
aa(1,2,3,4);
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/2F/4D/wKiom1OfCX3gtg6KAABdq8N2zkI114.jpg" title="jxj" alt="wKiom1OfCX3gtg6KAABdq8N2zkI114.jpg" />
function Hi() {
for(var i=0; i<arguments.length; i++) {
alert("Hi, " + arguments[i])
}
}
Hi("Cat", "Alice");
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/2F/4E/wKioL1OfCWOTdHZpAABjhJuZp9I621.jpg" title="4.jpg" alt="wKioL1OfCWOTdHZpAABjhJuZp9I621.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/2F/4D/wKiom1OfCafC-d4FAACDAS6we6Q715.jpg" title="5.jpg" alt="wKiom1OfCafC-d4FAACDAS6we6Q715.jpg" />
(2)另外一個屬性callee,該屬性是一個指標,指向擁有這個arguments對象的函數。
(3)另一個屬性calleer,該屬性指向調用當前函數的函數的引用。
Arguments.callee典型應用:
代碼1:
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) ;
}
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/2F/4E/wKioL1OfCZfSz3N_AACdpIeu1iE604.jpg" title="6.jpg" alt="wKioL1OfCZfSz3N_AACdpIeu1iE604.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/2F/4D/wKiom1OfCdSQIhNXAACo6jDQcDo384.jpg" title="7.jpg" alt="wKiom1OfCdSQIhNXAACo6jDQcDo384.jpg" />
這一章簡單介紹到這,後續幾篇文章,將介紹函數中最重要的屬性(沒有之一),prototype和很牛逼的兩個方法,apply,call();水平有限,請多多指教。
本文出自 “7439523” 部落格,請務必保留此出處http://7449523.blog.51cto.com/7439523/1427110