js arguments,jcallee caller用法總結

來源:互聯網
上載者:User

關鍵字:arguments,callee,caller
arguments:表示傳入函數的參數
callee:表示函數和函數主體的語句
caller:表示調用該函數的函數

arguments

該對象代表正在執行的函數和調用它的函數的參數。

caller

返回一個對函數的引用,該函數調用了當前函數。
functionName.caller
functionName 對象是所執行函數的名稱。

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

callee

返回正被執行的 Function 對象,也就是所指定的Function 對象的本文。

[function.]arguments.callee

可選項 function 參數是當前正在執行的 Function 對象的名稱。

說明

callee 屬性的初始值就是正被執行的 Function 對象。

callee 屬性是 arguments對象的一個成員,它表示對函數對象本身的引用,這有利於匿名函數的遞迴或者保證函數的封裝性,例如下邊樣本的遞迴計算1到n的自然數之和。而該屬性僅當相關函數正在執行時才可用。還有需要注意的是callee擁有length屬性,這個屬性有時候用於驗證還是比較好的。arguments.length是實參長度,arguments.callee.length是形參長度,由此可以判斷調用時形參長度是否和實參長度一致。
複製代碼 代碼如下:
<script type='text/javascript'>
function test(x,y,z)
{
alert("實參長度:"+arguments.length);
alert("形參長度:"+arguments.callee.length);
alert("形參長度:"+test.length);
alert(arguments[ 0 ])        
alert(test[ 0 ])           // undefined 沒有這種用法

}

//test(1,2,3);
test(1,2,3,4);

/*
*  arguments不是數組(Array類)
*/
Array.prototype.selfvalue  =   1 ;
function  testAguments() {
    alert( " arguments.selfvalue= " + arguments.selfvalue);
}
alert("Array.sefvalue="+new Array().selfvalue);
testAguments();

/**/ /*
 * 示範函數的caller屬性.
 * 說明:(當前函數).caller:返回一個對函數的引用,該函數調用了當前函數
  */

function  callerDemo()  {
     if  (callerDemo.caller)  {
         var  a =  callerDemo.caller.arguments[ 0 ];
        alert(a);
    }   else   {
        alert( " this is a top function " );
    }
}
function  handleCaller()  {
    callerDemo();
}

 callerDemo();
 handleCaller("參數1","參數2");


/**/ /*
 * 示範函數的callee屬性.
 * 說明:arguments.callee:初始值就是正被執行的 Function 對象,用於匿名函數
  */
function  calleeDemo()  {
    alert(arguments.callee);
}
 calleeDemo();
 (function(arg0,arg1){alert("形數數目為:"+arguments.callee.length)})();


/**/ /*
 * 示範apply,call函數的用法
 * 說明:作用都是將函數綁定到另外一個對象上去運行,兩者僅在定義參數方式有所區別:
 *       apply(thisArg,argArray);
 *     call(thisArg[,arg1,arg2…] ]);
 *     即所有函數內部的this指標都會被賦值為thisArg
  */

  function  ObjectA() {
    alert( " 執行ObjectA() " );
    alert(arguments[ 0 ]);
     this .hit = function (msg) {alert(msg)}
     this .info = " 我來自ObjectA "
 }

  function  ObjectB() {
    alert( " 執行ObjectB() " );
     // 調用ObjectA()方法,同時ObjectA建構函式中的所有this就會被ObjectB中的this替代
    ObjectA.apply( this ,arguments); // ObjectA.call(this);
    alert( this .info);
 }
  ObjectB('參數0');


  var  value = " global 變數 " ;
  function  Obj() {
     this .value = " 對象! " ;
 }
  function  Fun1() {
    alert( this .value);
 }
   Fun1();
   Fun1.apply(window);
   Fun1.apply(new Obj());

</script>

聯繫我們

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