JavaScriptのarguments,callee,caller,call,appy

2031 ワード

<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)
//argTest(1,2,3,4,5)

/*
 *  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>