Home > Article > Web Front-end > Introduction and examples of caller and callee attributes in js_Basic knowledge
1. caller
Returns a reference to the function that called the current function.
functionName.caller
The functionName object is the name of the executed function.
Description
For functions, the caller attribute is only defined when the function is executed. If the function is called from the top level of a Javascript program, then caller contains null .
The following example illustrates the usage of the caller attribute:
2. callee
Returns the Function object being executed, which is the body of the specified Function object.
[function.]arguments.callee
Optional function parameter is the name of the Function object currently being executed.
Description
The initial value of the callee attribute is the Function object being executed.
The callee attribute is a member of the arguments object, which represents a reference to the function object itself. This is beneficial to the recursion of anonymous functions or to ensure the encapsulation of functions. For example, the following example recursively calculates the sum of natural numbers from 1 to n. This property is only available when the relevant function is executing. It should also be noted that callee has a length attribute, which is sometimes better for verification. arguments.length is the actual parameter length, and arguments.callee.length is the formal parameter length. From this, you can determine whether the formal parameter length is consistent with the actual parameter length when calling.
Example