First, callee
Before you learn callee, you need to learn arguments first.
Arguments
- Meaning: The object represents the function being executed and the parameters of the function that called it.
- Grammar:
[function.] Arguments[n]
Parameter: function: The name of the function object that is currently executing.
N: the 0-based parameter value index to pass to the Function object.
And the first one is a property that needs to be learned today. Let's look at an example:
function Add (A, b) {console.log (Arguments.callee); return a+b;} Add (3,4);
Results:
As you can see from the results, callee is a pointer to the function that owns the arguments object. So what can you do with this property? Let's look at an example:
The factorial of function FAC (num) {if (num <= 1) {//0 is also 1return 1;} Else{return NUM*FAC (num-1);}} var TRUEFAC = FAC;FAC = function (num) {return 0;}; Console.log (TRUEFAC (10));
Results:
The result is not what we want, and the reason for this result is that FAC,FAC () is changed back to 0 forever, whereas the TRUEFAC () method uses the FAC () method, resulting in a result of 0. To solve this problem, you can use the Arguments.callee property.
Change the code to:
The factorial of function FAC (num) {if (num <= 1) {//0 is also 1return 1;} Else{return Num*arguments.callee (num-1);}} var TRUEFAC = FAC;FAC = function (num) {return 0;}; Console.log (TRUEFAC (10));
The result is:
Arguments.callee points to the owning function reference of the arguments object, and when the FAC function reference is assigned to TRUEFAC, the owning function of the arguments object becomes TRUEFAC, so the result is correct.
Second, caller
Unlike callee, the caller property is not part of the arguments object, it is a property of the function object and is not supported in earlier versions of opera, which holds a reference to the function that called the current function caller.
Example:
function outer () {inner ();} function inner () {console.log (Inner.caller);} Outer ();
Results:
The result shows that because outer () calls inner (), Inner.caller points to outer ().
The difference between Javascript Arguments.callee and caller