Differences between caller and callee and demonstration results

Source: Internet
Author: User

Caller

Caller returns a reference to a function that calls the current function.

Note that:

1. This attribute is only useful when a function is executed.
2. If a function is called by the top layer in a javascript program, null is returned.

FunctionName. caller: functionName is the currently executed function.
Copy codeThe Code is as follows:
Var a = function (){
Alert (a. caller );
}
Var B = function (){
A ();
}
B ();

In the above Code, B calls a, and a. caller returns a reference of B. The result is as follows:

If you call a directly (that is, a is called in any function, that is, the top-level call), return null:
Copy codeThe Code is as follows:
Var a = function (){
Alert (a. caller );
}
Var B = function (){
A ();
}
// B ();
A ();

Output result:

Callee

Callee puts back the reference of the function itself being executed. It is an attribute of arguments.

Pay attention when using callee:

1. This attribute is valid only when the function is executed.
2. It has a length attribute that can be used to obtain the number of parameters. Therefore, it can be used to compare whether the number of parameters and real parameters are consistent, that is, to compare whether arguments. length is equal to arguments. callee. length
3. It can be used to recursive anonymous functions.
Copy codeThe Code is as follows:
Var a = function (){
Alert (arguments. callee );
}
Var B = function (){
A ();
}
B ();

A is called in B, but it returns a reference. The result is as follows:

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.