Some friends may ask caller, What Is callee? What is the role of javascript? So this article will give some basic introduction to this. It is expected to help you understand callee and caller in javascript. Recently I learned javascript and encountered problems with caller and callee. I went to Baidu many times online. The searched content is shared with you.
Caller: returns a reference to the function that calls the function (usage: function. caller)
Note: For a function, the caller attribute is defined only when the function is executed. If the function is called by the top layer, caller is null.
Var time = 3 // The number of times to be controlled. If this parameter is removed, the caller and handleCaller will always Execute function caller () {caller. caller () // return function reference for calling caller function} function handleCaller () {if (time> 0) {time -- alert (handleCaller. caller) // return the reference to the alert (caller. caller) // return the call caller function reference caller ()} handleCaller ()
Example: When handleCaller runs for the first time, both alert return null, alert (handleCaller. caller) returns null because it is called by the top layer, alert (caller. caller) returns null because the default value of caller is null. Next, the caller () function is called. caller. caller returns a reference to the handleCaller function. caller. caller () can call the handleCaller function again. During the second handleCaller operation, alert (handleCaller. caller) returns the caller Code (which is actually a reference of caller) and alert (caller. caller) returns the handleCaller code. Because the call relationship between functions is handleCaller-> caller-> handleCaller. Then, the two functions are repeatedly executed.
Caller points to the function that calls the current function. However, if it is called within the global scope (that is, the top-level window), null is returned.
Start code
====================function testCaller(){if(testCaller.caller == null){console.log('accessed at global');}else{console.log('accessed at ' + testCaller.caller);}}
Global Call
testCaller(); // accessed at global
Call in a function
function a(){testCaller();}a(); // accessed at function a(){testCaller();}
In this case, testCaller. caller points to function.
Callee: returns the function reference of the corresponding arguments. (Mostly used for anonymous function recursion)
Note: callee returns the reference of the function being executed most frequently on the Internet. In my understanding, each function has its own arguments, which is usually used to store parameters. Arguments has a callee attribute. The initial value is a reference to its own function. When your function executes this statement, arguments is the current function by default, so arguments. callee is a reference to the currently executed function. Of course, if you have marked arguments of other functions (args in this example), you can 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: arguments. callee returns the reference of the currently executed function by default (a returns the reference of a's own function, c returns the reference of c's own function), and stores the arguments of function a by using args, use args in Built-in Function c. callee () calls function a again.
====================function a(x){if(x<=1)return x;elsereturn x + a(x-1);}a(12) // 78
This is a simple recursion and the running result is normal.
Let's take a look at the following call method.
Var B = a; a = null; // reclaim a from B (12); // erro: 'A' is not a function
The reason is also simple. B = a, B = function a () {}; before B calls, we use a = null. So when function a is running, a in return x + a (x-1); points to null, not function.
So an error is reported. How can this problem be solved. Let's change the expression.
Function a (x) {if (x <= 1) return x; elsereturn arguments. callee (x-1); // where this sentence is changed}
Call again
var b = a;a = null;b(12); // 78
Cause: although we set a = null, function a does not use a, but points to the current function through arguments. callee.
Because the definition of arguments. callee is: return the function being executed.