Analysis of the difference between callee and caller in JavaScript

Source: Internet
Author: User

Analysis of the difference between callee and caller in JavaScript

Some small partners may ask Caller,callee what is the role in JavaScript? Then this article will do some basic introduction. Hopefully it will help you understand the callee and caller in JavaScript.

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:

?

1 2 3 4 5 6 7 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

?

1 2 3 4 5 6 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.

?

1 2 3 4 5 6 7 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

?

1 2 3 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:

?

1 2 3 4 5 6 7 (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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.