Caller:
FunctionName. caller returns the caller.
Let's take a look at the following functions. You can copy them to VS and execute them.Copy codeThe 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.Copy codeThe 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.Copy codeThe 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.Copy codeThe 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.