Js arguments, jcallee caller usage Summary

Source: Internet
Author: User

Keywords: arguments, callee, caller
Arguments: parameters of the Input Function
Callee: the statement that represents the function and the function body.
Caller: indicates the function that calls this function.

Arguments

This object represents the function being executed and the parameters of the function that calls it.

Caller

Returns a reference to the function that calls the current function.
FunctionName. caller
The functionName object is the name of the executed function.

Description
For a function, the caller attribute is defined only when the function is executed. If the function is called by the top layer, caller contains null. If the caller attribute is used in the string context, the result is the same as functionName. toString, that is, the decompilation Text of the function is displayed.

Callee

Returns the Function object being executed, that is, the body of the specified Function object.

[Function.] arguments. callee

The optional function parameter is the name of the currently executed Function object.

Description

The initial value of the callee attribute is the Function object being executed.

The callee attribute is a member of the arguments object. It indicates a reference to the function object itself. This facilitates recursion of anonymous functions or ensures function encapsulation, for example, the following example recursively calculates the sum of natural numbers from 1 to n. This attribute is only available when the related function is being executed. It should also be noted that callee has the length attribute, which is sometimes used for verification. Arguments. length is the length of the real parameter, and arguments. callee. length is the length of the form parameter, which can be used to determine whether the length of the form parameter is consistent with the actual length.
Copy codeThe Code is as follows:
<Script type = 'text/javascript '>
Function test (x, y, z)
{
Alert ("real parameter length:" + arguments. length );
Alert ("parameter length:" + arguments. callee. length );
Alert ("parameter length:" + test. length );
Alert (arguments [0])
Alert (test [0]) // undefined does not have this usage

}

// Test (1, 2, 3 );
Test (1, 2, 3, 4 );

/*
* 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 ){
Var a = 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 (){
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>

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.