欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js arguments,jcallee caller用法總結(jié)

 更新時(shí)間:2013年11月30日 14:39:22   作者:  
這篇文章主要介紹了js中arguments, jcallee caller用法。需要的朋友可以過來參考下,希望對大家有所幫助

關(guān)鍵字:arguments,callee,caller
arguments:表示傳入函數(shù)的參數(shù)
callee:表示函數(shù)和函數(shù)主體的語句
caller:表示調(diào)用該函數(shù)的函數(shù)

arguments

該對象代表正在執(zhí)行的函數(shù)和調(diào)用它的函數(shù)的參數(shù)。

caller

返回一個(gè)對函數(shù)的引用,該函數(shù)調(diào)用了當(dāng)前函數(shù)。
functionName.caller
functionName 對象是所執(zhí)行函數(shù)的名稱。

說明
對于函數(shù)來說,caller屬性只有在函數(shù)執(zhí)行時(shí)才有定義。如果函數(shù)是由頂層調(diào)用的,那么 caller包含的就是 null 。如果在字符串上下文中使用 caller 屬性,那么結(jié)果和functionName.toString一樣,也就是說,顯示的是函數(shù)的反編譯文本。

callee

返回正被執(zhí)行的 Function 對象,也就是所指定的Function 對象的正文。

[function.]arguments.callee

可選項(xiàng) function 參數(shù)是當(dāng)前正在執(zhí)行的 Function 對象的名稱。

說明

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

callee 屬性是 arguments對象的一個(gè)成員,它表示對函數(shù)對象本身的引用,這有利于匿名函數(shù)的遞歸或者保證函數(shù)的封裝性,例如下邊示例的遞歸計(jì)算1到n的自然數(shù)之和。而該屬性僅當(dāng)相關(guān)函數(shù)正在執(zhí)行時(shí)才可用。還有需要注意的是callee擁有l(wèi)ength屬性,這個(gè)屬性有時(shí)候用于驗(yàn)證還是比較好的。arguments.length是實(shí)參長度,arguments.callee.length是形參長度,由此可以判斷調(diào)用時(shí)形參長度是否和實(shí)參長度一致。

復(fù)制代碼 代碼如下:

<script type='text/javascript'>
function test(x,y,z)
{
alert("實(shí)參長度:"+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不是數(shù)組(Array類)
*/
Array.prototype.selfvalue  =   1 ;
function  testAguments() {
    alert( " arguments.selfvalue= " + arguments.selfvalue);
}
alert("Array.sefvalue="+new Array().selfvalue);
testAguments();

/**/ /*
 * 演示函數(shù)的caller屬性.
 * 說明:(當(dāng)前函數(shù)).caller:返回一個(gè)對函數(shù)的引用,該函數(shù)調(diào)用了當(dāng)前函數(shù)
  */

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("參數(shù)1","參數(shù)2");


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


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

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

  function  ObjectB() {
    alert( " 執(zhí)行ObjectB() " );
     // 調(diào)用ObjectA()方法,同時(shí)ObjectA構(gòu)造函數(shù)中的所有this就會被ObjectB中的this替代
    ObjectA.apply( this ,arguments); // ObjectA.call(this);
    alert( this .info);
 }
  ObjectB('參數(shù)0');


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

</script>

相關(guān)文章

最新評論