js函數的內部屬性---arguments,callee,caller

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.