The difference between caller and callee

Source: Internet
Author: User

①, caller
Caller returns a reference to the function that called the current function.
Use this attribute to note:
     1 This property is used only when the function is executed
2 If the function is called by the top-level in a JavaScript program, it returns null

FunctionName.caller:functionName is a function that is currently executing.
    Example 1: Copy the Code code as follows:
    
       varfunction() {alert (A.caller);           }  varfunction() {a (); } b ();
In the above code, B calls A, then A.caller returns a reference to B, the result is as follows: Example 2: return null
<! DOCTYPE html>var function () {                alert (a.caller);            }            A ();        </script>    

②, callee

Callee returns a reference to the function itself that is being executed, which is a property of the arguments
Note When using callee:
    1 This property is valid only if the function is executed
2 It has a length property, which can be used to get the number of formal parameters, so it can be used to compare the number of parameters and arguments, that is, whether the comparison arguments.length equals arguments.callee.length
3 It can be used to recursively return anonymous functions.

Example 1: Return a reference to the function itself

var a = function () {
alert (Arguments.callee);
}
var B = function () {
A ();
}
b ();

A is called in B, but it returns a reference to a itself, with the following result:

    

    Example 2: Getting the formal parameters of a function

Summary: Caller is a function that returns a reference to the function that is called callee is returned itself

The difference between caller and callee

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.