[zz]javascript的arguments,caller,callee,call,apply範例及理解

來源:互聯網
上載者:User

引文詳見:JavaScript中的arguments,callee,caller,call,appy

代碼及理解如下:

code<script language="JavaScript">
/**//*
 * 示範arguments的用法,如何擷取實參數和形數數
 */
function argTest(a,b,c,d){
    var numargs = arguments.length;     // 擷取被傳遞參數的數值。
    var expargs = argTest.length;       // 擷取期望參數的數值。
    alert("實參數目為:"+numargs)
    alert("形數數目為:"+expargs)

    alert(arguments[0])         
    alert(argTest[0])          //undefined 沒有這種用法
}
//argTest(1,2)
//結果:實參數目為:2;形數數目為:4;1;undefined
//argTest(1,2,3,4,5)
//結果:實參數目為:5;形數數目為:4;1;undefined
/*總結:js參數傳遞不像c#等文法要求的必須與函數定義一致,可以根據應用不同增加或減少參數,極端情況可以不聲明參數,如*/
    var test = function(){
        var a = 0;
        var l = 0;
        while(l<arguments.length)
        {
            a += arguments[l];
            l++;
        }
        alert(a);
    }
    test();//0
    test(1,2);//3

/**//*
 *  arguments不是數組(Array類)
 */

Array.prototype.selfvalue = 1;
//此處就是為人詬病的渲染原聲對象吧
function testAguments(){
    alert("arguments.selfvalue="+arguments.selfvalue);
}
//alert("Array.sefvalue="+new Array().selfvalue);
/*上面的調用說明所有Array對象的selfvalue都等於1*/
//testAguments();
/*結果undefined說明arguments並非Array類型*/

/**//*
 * 示範函數的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();
//結果:this is a top function
//handleCaller("參數1","參數2");
//結果:參數1
/*總結:caller應用於函數對象,可以取到上層調用的函數對象及參數
*/

/**//*
 * 示範函數的callee屬性.
 * 說明:arguments.callee:初始值就是正被執行的 Function 對象,用於匿名函數
 */
function calleeDemo() {
    alert(arguments.callee);
}
//calleeDemo();
//(function(arg0,arg1){alert("形數數目為:"+arguments.callee.length)})();
/*總結:可用於實現遞迴,如:*/
       var i = 0;
    var a = function(){
        i += arguments[0];
        if(i>100||i==0)
            alert(i);
        else
            arguments.callee(i);
    };
    a(1);
//結果128,其實就是2的7次方

/**//*
 * 示範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');
/*總結:通常同callee聯用*/
var i = 0
function a(b)
{
    i++;
    if(i>4)
        return i;
    else
        return arguments.callee.call(this,b);
}
alert(a(0));

//講述this範圍的問題

 var value="global 變數";
 function Obj(){
    this.value="對象!";
 }
 function Fun1(){
    alert(this.value);
 }
 //Fun1();
//結果:global 變數
 //Fun1.apply(window);
//結果:global 變數,預設this=window
 //Fun1.apply(new Obj());
//結果:對象,this=new Obj();Obj中的this指的是函數對象執行個體
</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.