Arguments. callee arguments. Caller call aply

Source: Internet
Author: User
Reposted from arguments, callee, caller, call, And appy in Javascript, a small programmer reads an article on the Internet <fully understand the concepts of JavaScript caller, callee, call, and apply>, write down the code.
<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 value of the expected parameter.
Console. Log ("the number of real parameters is:" + numargs)
Console. Log ("Quantity:" + expargs)

Console. Log (arguments [0])
Console. Log (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 (){
Console. Log ("arguments. selfvalue =" + arguments. selfvalue );
}
// Console. Log ("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 ){
VaR A = callerdemo. Caller. Arguments [0];
Console. Log ();
} Else {
Console. Log ("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 (){
Console. Log (arguments. callee );
}
// Calleedemo ();
// (Function (arg0, arg1) {console. Log ("Quantity:" + 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. (calling a function within a specific scope is actually equivalent to setting the this value in the function body.) The two 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 (){
Console. Log ("execute objecta ()");
Alert (arguments [0]);
This. Hit = function (MSG) {alert (MSG )}
This.info = "I'm from objecta"
}
 
Function objectb (){
Alert ("execute objectb ()");
// Call the objecta () method, and 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>

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.