Js--callee and Caller

Source: Internet
Author: User

Callee

Callee is a property of an object that is a pointer to a function that points to a parameter arguments object
First, let's write a step-up function:

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

From this function can be seen, the use of recursive function, if changed the function name, inside the function name also with the change, this is very 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 this is written: according to the definition of callee, it can be seen that callee is a property of the arguments object, a function that points to the arguments object, which is Chen (chen=arguments.callee ), so the explanation should be understandable.

Caller

Caller is a property of a function object that holds a reference to the function that called the current function ( the direct parent function that points to the current function)

Let's start with an example.

function A () {B ();}; Function B () {alert (b.caller);}; A (); The result is a popup function A and the contents

Let's explain, first of all, the property of function B caller calls the function of the current function B to reference a (that is, to the parent function A of the current function B), so the result is a popup function a () {B ();};

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

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

From this code you can see that the B function called the B function name, so that when the function name changes is inconvenient, we need to replace the inside of the B
Before we know what method can point to the current object, let's modify it as follows:

(function A () {B ();}) (); function B () {alert (arguments.callee.caller);};

Js--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.