Usage and application of callee and caller in JavaScript _javascript techniques

Source: Internet
Author: User
Caller:
Functionname.caller returns the caller.

Take a look at the following function, you can copy it to vs under Execute
Copy Code code as follows:

function caller () {
if (Caller.caller) {
Alert (caller.caller.toString ());
} else {
Alert ("Function direct execution");
}
}
function Handlecaller () {
Caller ();
}
Handlecaller ();
Caller ();


You will find that the first alert pops up the caller Handlecaller calling the caller function, and the second alert executes alert ("Function direct execution") because it is not called in other functions, so caller is null.


Callee:


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

The recursion of a function or the encapsulation of an assurance function. The following code first describes the use of callee, and the instance code is excerpted from the Web
Copy Code code as follows:

function Calleelengthdemo (arg1, arg2) {
Alert (arguments.callee.toString ());
if (arguments.length = = arguments.callee.length) {
Window.alert ("Verify parameter and argument length is correct!") ");
Return
} else {
Alert ("Actual parameter length:" + arguments.length);
Alert ("Parameter length:" + arguments.callee.length);
}
}
Calleelengthdemo (1);

The first message box pops up the Calleelengthdemo function itself, which means that callee is a reference to the object of the function itself. Callee also has a very useful application to determine whether the actual parameter is consistent with the line parameters. The first message box in the above code pops up the length of the actual parameter is 1, the formal parameter is the parameter length of the function itself is 2.



Application Scenario:
Callee application scenarios are typically used for anonymous functions
Let's take a look at the following code excerpt from the network
Copy Code code as follows:

var fn=function (n) {
if (n>0) return N+FN (n-1);
return 0;
}
Alert (FN (10))

The function contains a reference to itself, and the function name is just a variable name, which is equivalent to calling the
A global variable, it is not very good to reflect the call itself, then use callee would be a better way
Copy Code code as follows:

var fn= (function (n) {
if (n>0) return N+arguments.callee (n-1);
return 0;
}) (10);
Alert (FN)

This allows the code to be more concise. It also prevents the pollution of global variables.

Caller's application scenario is primarily used to see 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.