Recently learn JavaScript, encounter caller and callee problems, go online Baidu a lot. Found the content of Datong small benefit, collation summed up a bit to share with you.
Caller: Returns a reference to a function that calls a functions function (usage: function.caller)
Note: For functions, the caller property is defined only when the function executes. If the function is called by the top level, caller is null.
var time = 3//control times, remove will always be in caller and Handlecaller alternately execute function
caller () {
caller.caller ()//Return functions reference to call caller function
}
function Handlecaller () {
if (Time > 0) {
time--
alert (handlecaller.caller)//Return functions reference to call Handlecaller function
alert (Caller.caller)//Returns a function reference to the call caller function
caller ()
}
}
Handlecaller ()
Example analysis: The first time the Handlecaller run, two alert returns Null,alert (Handlecaller.caller) returns NULL because it is called by the top-level, alert (Caller.caller) Null is returned because the default value for caller is null. The next caller () function is invoked, Caller.caller returns a reference to the function (Handlecaller) that invoked it, and the Handlecaller function can be called again by Caller.caller (). When the second Handlecaller runs, alert (handlecaller.caller) returns the caller code (in fact, the caller reference), alert (Caller.caller) Returns the Handlecaller code. Because the invocation relationship between functions is Handlecaller->caller->handlecaller. It is then performed alternately between 2 functions.
Caller points to a function that invokes the current function, but there is one point where it returns null if it is called in the global scope (that is, the top-level window).
Code goes up
====================
function Testcaller () {
if (Testcaller.caller = null) {
console.log (' accessed at Global ');
} else{
console.log (' accessed at ' + Testcaller.caller);
}
In the global call
Testcaller (); Accessed at global
Called in a function
function A () {
testcaller ();
}
A (); Accessed at function A () {Testcaller ();}
At this point, the Testcaller.caller point is function A.
Callee: Returns a function reference for the corresponding arguments. (More for anonymous function recursion)
Description: Perhaps the most you see on the Internet is callee returns the function reference being executed. As I understand it, each function has a arguments of its own, which is usually used to store parameters. Arguments has a callee property, the initial value is the corresponding function reference. When your function executes to the statement, arguments is the default corresponding to the function you are performing now, then Arguments.callee is a reference to the function that is currently executing. Of course, if you have arguments (args in the example) that mark other functions, you can naturally use Args.callee () to call that function again.
function A () {
alert (Arguments.callee)
var args = arguments
function C () {
alert (Arguments.callee)
Args.callee ()
}
C ()
}
A ()
Example analysis: The Arguments.callee in the example is the default return reference to the currently executing function (a returns the function reference of a itself, C returns a reference to its own function), and by storing the arguments of a function with args, Use Args.callee () in the built-in function C to call the A function again.
====================
function A (x) {
if (x<=1) return
x;
else return
x + A (x-1);
}
A (12)//78
This is a minimalist recursion, the results of normal operation.
And look at the following call method
var b = A;
A = null; Recycle a
(b);//Erro: ' A ' is not a function
The reason is also simple, b=a,b=function A () {}; before the b call, we used a=null. So when function A is running, the return x + A (x-1) in which it points is null, not function A.
So the error, how to solve such a problem. We'll put a in a different way.
function A (x) {
if (x<=1) return
x;
else return
Arguments.callee (x-1);//This is the change place
}
and then call
var b = A;
A = null;
B (12); 78
Reason: Although we will be a=null, but function A does not use a, but by Arguments.callee point to the current function.
Because Arguments.callee is defined as: Returns the function being executed.