JavaScript中callee,caller,argument的理解

來源:互聯網
上載者:User

標籤:

argument代表當前函數的參數數組;

1、callee的用法:

argument.callee表示誰引用的這個函數

其他解釋:(arguments.callee表示引用當前正在執行的函數,或者說是調用arguments.callee的函數對象的引用,它給匿名函數提供了一種自我引用的方式。通過如下例子,可以更好的理解概念。)

例如:var fun=function(){

  console.log(fun===argument.callee)

}

fun();   //true;

執行上述代碼,可以看到alter出來的結果是true,注意,此處用的是“===”,就是說func與arguments.callee物件類型和值都相等。

用在遞迴上,例如:

var r2=(function(n){

  if(n>1){

    return n*argument.callee(n-1);

  }else{

    return 1;

  }

})(n)

 

 2、caller的用法:

返回一個函數的引用,該函數調用了當前函數

functionName.claaer

functionName 對象是所執行函數的名稱

對於函數來說,claller只有在函數被調用時才有定義,如果函數是有頂層調用的,那麼caller包含的就是null,如果在字串上下文中調用,那麼caller屬性,那麼結果和 functionName.toString 一樣,也就是說,顯示的是函數的反編譯文本。

 

代碼

 

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// caller demo {
function callerDemo() {
if (callerDemo.caller) {
var a= callerDemo.caller.toString();
alert(a);
} else {
alert("this is a top function");
}
}
function handleCaller() {
callerDemo();
}

 

handleCaller();//彈出handleCaller的定義

 

 

JavaScript中callee,caller,argument的理解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.