Arguments, callee, caller, call, appy in Javascript

Source: Internet
Author: User
< 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 >

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.