The difference between Javascript Arguments.callee and caller

Source: Internet
Author: User

First, callee

Before you learn callee, you need to learn arguments first.

Arguments

    • Meaning: The object represents the function being executed and the parameters of the function that called it.
    • Grammar:
[function.] Arguments[n]

Parameter: function: The name of the function object that is currently executing.

N: the 0-based parameter value index to pass to the Function object.

    • Description: In the previous execution context of the learning process, it is known that the build execution context requires two stages, respectively, into the execution context and execution phase. There are three tasks that need to be done in the context of entering execution, one of which is to create and initialize AO, the arguments object.
    • Arguments Objects is an inner object within the function context of the active object AO, which includes the following properties:

      1. Callee: Reference to the current function
      2. Length: The number of parameters that are actually passed
      3. Properties-indexes: is the parameter value of the function (sorted from left to right by argument list)

And the first one is a property that needs to be learned today. Let's look at an example:

function Add (A, b) {console.log (Arguments.callee); return a+b;} Add (3,4);

Results:

  

As you can see from the results, callee is a pointer to the function that owns the arguments object. So what can you do with this property? Let's look at an example:

The factorial of function FAC (num) {if (num <= 1) {//0 is also 1return 1;} Else{return NUM*FAC (num-1);}} var TRUEFAC = FAC;FAC = function (num) {return 0;}; Console.log (TRUEFAC (10));

Results:

The result is not what we want, and the reason for this result is that FAC,FAC () is changed back to 0 forever, whereas the TRUEFAC () method uses the FAC () method, resulting in a result of 0. To solve this problem, you can use the Arguments.callee property.

Change the code to:

The factorial of function FAC (num) {if (num <= 1) {//0 is also 1return 1;} Else{return Num*arguments.callee (num-1);}} var TRUEFAC = FAC;FAC = function (num) {return 0;}; Console.log (TRUEFAC (10));

The result is:

Arguments.callee points to the owning function reference of the arguments object, and when the FAC function reference is assigned to TRUEFAC, the owning function of the arguments object becomes TRUEFAC, so the result is correct.

Second, caller

Unlike callee, the caller property is not part of the arguments object, it is a property of the function object and is not supported in earlier versions of opera, which holds a reference to the function that called the current function caller.

Example:

function outer () {inner ();} function inner () {console.log (Inner.caller);} Outer ();

Results:

 

The result shows that because outer () calls inner (), Inner.caller points to outer ().

  

The difference between Javascript Arguments.callee and caller

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.