< Script Language = " Javascript " >
/*
* Demonstrate the usage of arguments and how to obtain real parameters and number of shapes
*/
Function Argtest (A, B, C, D) {
VaR Numargs = Arguments. length; // Obtain the value of the passed parameter.
VaR Expargs = Argtest. length; // Obtain the expected value.
Alert ( " The number of real parameters is: " + Numargs)
Alert ( " Number of shapes: " + Expargs)
Alert (arguments [ 0 ])
Alert (argtest [ 0 ]) // Undefined does not have this usage
}
// Argtest (1, 2)
// Argtest (1, 2, 3, 4, 5)
/*
* Arguments is not an array (array class)
*/
Array. Prototype. selfvalue = 1 ;
Function Testaguments () {
Alert ("Arguments. selfvalue ="+Arguments. selfvalue );
}
// Alert ("array. sefvalue =" + new array (). selfvalue );
// Testaguments ();
/*
* Demonstrate the caller attribute of the function.
* Description: (current function). Caller: returns a reference to the function, which calls the current function.
*/
Function Callerdemo () {
If (Callerdemo. Caller) {
VaRA=Callerdemo. Caller. Arguments [0];
Alert ();
} Else {
Alert ("This is a top Function");
}
}
Function Handlecaller () {
Callerdemo ();
}
// Callerdemo ();
// Handlecaller ("parameter 1", "parameter 2 ");
/*
* Shows the callee attribute of the function.
* Description: arguments. callee: the initial value is the function object being executed. It is used for anonymous functions.
*/
Function Calleedemo () {
Alert (arguments. callee );
}
// Calleedemo ();
// (Function (arg0, arg1) {alert ("Number of shapes:" + arguments. callee. Length )})();
/*
* Demonstrate the usage of the apply and call Functions
* Note: The function is used to bind a function to another object for running. The two functions are different only when defining parameters:
* Apply (thisarg, argarray );
* Call (thisarg [, arg1, arg2…] ]);
* That is, the this pointer inside all functions will be assigned to thisarg.
*/
Function Objecta () {
Alert ( " Execute objecta () " );
Alert (arguments [ 0 ]);
This . Hit = Function (MSG) {Alert (MSG )}
This . Info = " I'm from objecta "
}
Function Objectb () {< br> alert ( " execute objectb () " );
/// call the objecta () method, at the same time, all this in the objecta constructor will be replaced by this in objectb
objecta. apply ( This , arguments ); // objecta. call (this);
alert ( This . info);
}
// Objectb ('parameter 0 ');
VaR Value = " Global variable " ;
Function OBJ () {
This. Value="Object!";
}
Function Fun1 () {
Alert (This. Value );
}
// Fun1 ();
// Fun1.apply (window );
// Fun1.apply (New OBJ ());
</ Script >