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.