callee
callee is an attribute of the object, which is a pointer pointing to the function of the arguments object
First, let’s write an order function:
function chen(x){ if (x<=1) { return 1; } else{ return x*chen(x-1); }; };
As can be seen from this function, a recursive function is used. If the function name is changed, the function name inside will also change. This is very inconvenient, so we use callee to try it
function chen(x){ if (x<=1) {return 1; }else{ return x*arguments.callee(x-1); }; };
Let’s analyze why it is written like this: According to the definition of callee, it can be seen that callee is an attribute of the arguments object and points to a function of the arguments object. This function is chen (chen=arguments.callee). This explanation should be understandable. Come on.
caller
caller is an attribute of the function object, which holds a reference to the function that calls the current function (pointing to the direct parent function of the current function)
Let’s take an example first
function a(){ b(); }; function b(){ alert(b.caller); }; a(); //结果就是弹出函数a和内容
Let’s explain it. First, the attribute caller of function b calls the function reference a of the current function b (which points to the parent function a of the current function b), so the result is that function a(){ b();}; pops up;
Now that you understand caller and callee, can you combine the two to use them?
function b(){ alert(b.caller); };
From this code, we can see that the b function name is called in the b function. This is very inconvenient when the function name changes. We need to replace the b
Previously we knew how to point to the current object, let’s modify it next:
(function a(){ b(); })(); function b(){ alert(arguments.callee.caller); };
As can be seen from the code, we replaced the b function with arguments.callee, so the trouble was solved. . . . .
The above is the entire content of this article, I hope you all like it.