Differences between caller and callee

Source: Internet
Author: User

Differences between caller and callee

Caller returns a reference to a function, which calls the current function; callee puts back the reference of the function itself being executed, which is an attribute of arguments

Caller
Caller returns a reference to a function that calls the current function.
Note the following before using this attribute:
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.

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:

var b = function() { a(); } 
If you call a directly (that is, a is called in any function, that is, the top-level call), return null:

var a = function() { alert(a.caller); } var b = function() { a(); } //b(); a();
Output result:

Null

Callee
Callee puts back the reference of the function itself being executed. It is an attribute of arguments.
Note the following 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.

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:
var a = function() { alert(arguments.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.