caller :
functionName.caller returns the caller.
Look at the function below, you can copy it into VS and execute it
function caller() {
if (caller.caller) {
alert(caller.caller.toString());
} else {
alert("function Execute directly");
}
}
function handleCaller() {
caller();
}
handleCaller();
caller();
You will find that the first alert will pop up the caller handleCaller that calls the caller function, and the second alert is not called in other function bodies, so the caller is null, and the alert(" function is executed Direct execution");
callee:
Returns the Function object being executed, which is the body of the specified Function object.
callee is the arguments An attribute member that represents a reference to the function object itself, which facilitates the recursion of anonymous
functions or ensures the encapsulation of functions. The following code first explains the usage of callee. The example code is taken from the Internet
function calleeLengthDemo(arg1, arg2) {
alert(arguments.callee.toString());
if (arguments.length == arguments.callee.length) {
window.alert("Verify the shape The parameters and actual parameter lengths are correct! ");
return;
} else {
alert("Actual parameter length: " arguments.length);
alert("Formal parameter length: " arguments. callee.length);
}
}
calleeLengthDemo(1);
The first message box pops up the calleeLengthDemo function itself, which shows that callee is a reference to the function object itself. Another very useful application of callee is to determine whether the actual parameters are consistent with the row parameters. The first message box of the above code will pop up. The actual parameter length is 1, and the formal parameter, which is the parameter length of the function itself, is 2.
Application scenario:
callee application Scenarios are generally used for anonymous functions
Take a look at the following code excerpted from the Internet
var fn=function(n){
if(n>0) return n fn(n-1);
return 0;
}
alert(fn(10) )
The function contains a reference to itself. The function name is just a variable name. Calling it inside the function is equivalent to calling
a global variable, which cannot well reflect the call. itself, using callee would be a better method at this time
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 the pollution of global variables.
The application scenario of caller is mainly used to check which function the function itself is called.