Differences between callee and caller in javascript
Differences between callee and caller in javascript
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.
Callee
Callee is an attribute of an object. It is a pointer pointing to the function of the arguments object.
First, we will write a level-1 function:
?
1 2 3 4 5 6 7 |
Function chen (x ){ If (x <= 1 ){ Return 1; } Else { Return x * chen (x-1 ); }; }; |
It can be seen from this function that the recursive function is used. If the function name is changed, the function name in it will also change. This is inconvenient, so we can use callee to try it.
?
1 2 3 4 5 6 |
Function chen (x ){ If (x <= 1) {return 1; } Else { Return x * arguments. callee (x-1 ); }; }; |
Let's analyze the reason for writing: According to callee's definition, we can see that callee is an attribute of the arguments object and points to the function of the arguments object. This function is chen (chen = arguments. callee), so the explanation should be understandable.
Caller
Caller is an attribute of a function object. It stores the reference of the function that calls the current function (pointing to the direct parent function of the current function)
Let's take an example.
?
1 2 3 4 5 6 7 |
Function (){ B (); }; Function B (){ Alert (B. caller ); }; A (); // The result is the pop-up function a and content. |
Let's explain it. First, the caller of function B calls the function of the current function B to reference a (that is, to point to the parent function a of the current function B). Therefore, function a () is displayed () {B ();};
Now that we understand caller and callee, can we combine them?
?
1 2 3 |
Function B (){ Alert (B. caller ); }; |
From this code, we can see that function B is called in function B, which is inconvenient when the function name changes. We need to replace
We know what method can be used to point to the current object. Let's modify it as follows:
?
1 2 3 4 5 6 7 |
(Function (){ B (); })(); Function B (){ Alert (arguments. callee. caller ); }; |
From the code, we can see that we replaced the B function with arguments. callee, so it solved the problem .....
The above is all the content of this article. I hope you will like it.