JavaScript in caller and callee

Source: Internet
Author: User

Recently learned JavaScript, encountered caller and callee problems, to go online Baidu a lot. Search the content of Datong small benefits, finishing summed up and share with you.

Caller: Returns a reference to the function that called the functions function (usage: function.caller)

Description: for a function, the caller property is defined only when the function executes. If the function is called by the top level, caller is null.

var time = 3//control number, remove will always be in the caller and Handlecaller alternately continuously execute function  caller () {    caller.caller ()// Returns a function reference that invokes the caller function,} function handlecaller    () {if (Time > 0) {time        --        alert (handlecaller.caller)//Return function reference called Handlecaller function        alert (caller.caller)// Returns a function reference that calls the caller function        caller ()    }}handlecaller ()

Example Analysis: when the first Handlecaller run, two alert returns is Null,alert (Handlecaller.caller) returns NULL because it is called by the top level, alert ( Caller.caller) returns NULL because the default value of caller is null. Then the caller () function is called, Caller.caller returns a reference to the function (Handlecaller) that called it, and the Handlecaller function can be called again through Caller.caller (). The second time Handlecaller runs, alert (handlecaller.caller) returns the caller code (which is actually the caller reference), alert (Caller.caller) The Handlecaller code is returned. Because the call relationship between functions is Handlecaller->caller->handlecaller. This is followed by an alternating execution between 2 functions.

callee: Returns the function reference for the corresponding arguments. (More for anonymous function recursion)

Description: Perhaps the most you see on the net is callee returns the function reference being executed. I understand that each function has its own arguments, which is usually used to store parameters. Arguments has a callee property, and the initial value is the function reference corresponding to itself. When your function executes to the statement, arguments is the default corresponding to the function you are performing now, and Arguments.callee is a reference to the currently executing function. Of course, if you have the arguments (args in the example) labeled with other functions, you can naturally use Args.callee () to call that function again.

function A () {    alert (Arguments.callee)    var args = arguments    function  C () {        alert (Arguments.callee)        Args.callee ()    }    C ()}a ()

Example Analysis: The Arguments.callee in the example is the default return reference to the currently executing function (a returns a self-function reference in a, C returns the C self-function Reference), and by storing the arguments of the a function with args, Call the A function again using Args.callee () in the built-in function C.

JavaScript in caller and callee

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.