Analysis of the difference between callee and caller in JavaScript _javascript skills

Source: Internet
Author: User

Callee

Callee is a property of an object that is a pointer to a function of a parameter arguments object
Let's start by writing a step function:

function Chen (x) {
if (x<=1) {return
1;
} else{return
x*chen (x-1)
;;};

From this function can be seen that the use of recursive functions, if the change in the function name, the function name inside also change, so it is inconvenient so we use callee to try

function Chen (x) {
if (x<=1) {return 1;
} else{return
X*arguments.callee (x-1);
};

Let's analyze why we write this: according to the definition of callee, we can see that callee is a property of the arguments object, a function that points to the arguments object, this function is Chen (Chen=arguments.callee), This explanation should be understandable.

Caller

Caller is a property of a function object that holds a reference to the function that invokes the current function (pointing to the immediate parent function of the current function)

Let's start with an example.

function A () {
B ();
};
Function B () {
alert (b.caller);
};
A (); The result is pop-up function A and content

Let's explain, first of all, function B's property caller the function that calls the current function B refers to a (that is, a parent function of the current function B), so the result is a pop-up function a () {B ();};

So understand caller and callee, then you can not combine the two together to use it

Function B () {
alert (b.caller);
};

We can see from this code that the B function name is called in the B function, so it's inconvenient when the function name changes, we need to replace that B inside.
Before we know what method to point to the current object, we will modify the following:

(function A () {
B ();
}) ();

Function B () {
alert (arguments.callee.caller);
};

We can see from the code that we replaced the B function with Arguments.callee, so we solved the problem ...

The above mentioned is the entire content of this article, I hope you can enjoy.

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.