Differences between callee and caller in javascript

Source: Internet
Author: User

Differences between callee and caller in javascript

Differences between callee and caller in javascript

Some friends may ask caller, What Is callee? What is the role of javascript? So this article will give some basic introduction to this. It is expected to help you understand callee and caller in javascript.

Callee

Callee is an attribute of an object. It is a pointer pointing to the function of the arguments object.

First, we will write a level-1 function:

?

1

2

3

4

5

6

7

Function chen (x ){

If (x <= 1 ){

Return 1;

} Else {

Return x * chen (x-1 );

};

};

It can be seen from this function that the recursive function is used. If the function name is changed, the function name in it will also change. This is inconvenient, so we can use callee to try it.

?

1

2

3

4

5

6

Function chen (x ){

If (x <= 1) {return 1;

} Else {

Return x * arguments. callee (x-1 );

};

};

Let's analyze the reason for writing: According to callee's definition, we can see that callee is an attribute of the arguments object and points to the function of the arguments object. This function is chen (chen = arguments. callee), so the explanation should be understandable.

Caller

Caller is an attribute of a function object. It stores the reference of the function that calls the current function (pointing to the direct parent function of the current function)

Let's take an example.

?

1

2

3

4

5

6

7

Function (){

B ();

};

Function B (){

Alert (B. caller );

};

A (); // The result is the pop-up function a and content.

Let's explain it. First, the caller of function B calls the function of the current function B to reference a (that is, to point to the parent function a of the current function B). Therefore, function a () is displayed () {B ();};

Now that we understand caller and callee, can we combine them?

?

1

2

3

Function B (){

Alert (B. caller );

};

From this code, we can see that function B is called in function B, which is inconvenient when the function name changes. We need to replace

We know what method can be used to point to the current object. Let's modify it as follows:

?

1

2

3

4

5

6

7

(Function (){

B ();

})();

 

Function B (){

Alert (arguments. callee. caller );

};

From the code, we can see that we replaced the B function with arguments. callee, so it solved the problem .....

The above is all the content of this article. I hope you will like it.

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.