Usage and Application Scenario of callee and caller in javascript _ javascript skills

Source: Internet
Author: User
The usage and Application Scenario Analysis of callee and caller in javascript. For more information, see. Caller:
FunctionName. caller returns the caller.

Let's take a look at the following functions. You can copy them to VS and execute them.

The Code is as follows:


Function caller (){
If (caller. caller ){
Alert (caller. caller. toString ());
} Else {
Alert ("direct function execution ");
}
}
Function handleCaller (){
Caller ();
}
HandleCaller ();
Caller ();



You will find that the first alert will pop up the caller handleCaller who calls the caller function. The second alert is null because it is not called in other functions, the alert ("direct function execution") is executed ");


Callee:


Returns the Function object being executed, that is, the body of the specified Function object.
Callee is an attribute member of arguments. It indicates a reference to the function object itself, which facilitates anonymity.

Recursion of a function or encapsulation of a function. The following code first describes the usage of callee. The instance code is taken from the Internet.

The Code is as follows:


Function calleeLengthDemo (arg1, arg2 ){
Alert (arguments. callee. toString ());
If (arguments. length = arguments. callee. length ){
Window. alert ("verify that the length of the form parameter and real parameter is correct! ");
Return;
} Else {
Alert ("real parameter length:" + arguments. length );
Alert ("parameter length:" + arguments. callee. length );
}
}
CalleeLengthDemo (1 );


In the first message box, the calleeLengthDemo function itself is displayed, indicating that callee is a reference to the function object. Callee also has a very useful application to determine whether the actual parameters are consistent with the row parameters. In the first message box of the code above, the actual parameter length is 1, and the formal parameter is the parameter length of the function itself is 2.



Application scenarios:
Callee is generally used for anonymous functions.
The following code is taken from the network.

The Code is as follows:


Var fn = function (n ){
If (n> 0) return n + fn (n-1 );
Return 0;
}
Alert (fn (10 ))


A function contains a reference to itself. A function name is only a variable name.
A global variable cannot reflect the call itself. Using callee is a good method.

The Code is as follows:


Var fn = (function (n ){
If (n> 0) return n + arguments. callee (n-1 );
Return 0;
}) (10 );
Alert (fn)


This makes the code more concise. It also prevents global variable pollution.

The caller application scenario is mainly used to check which function is called by the function itself.
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.