Actual parameter object of JavaScript function (arguments)/callee Property/Caller Property/method of recursive call/Get function name

Source: Internet
Author: User

Scope of function: calling Object

The body of a function in JavaScript is performed in a local scope, which differs from the global scope. This new scope is created by adding the calling object to the head of the scope chain (this sentence is not understood, the understanding of the pro can leave a message to tell me, thank you). Because the calling object is part of the scope chain, the object property can be accessed as a variable in the body of the function.

The properties of the calling object include: Local variables declared with Var, function parameters, and a special property arguments

Actual parameters of the function: actual Parameter object

A arguments object that is used to reference the actual parameter object. The arguments object of a function is not an array, and a single parameter is accessed in the same way as an array element. Index n is actually 0 of the arguments object ... One of the parameters of the N property.

( function f () {// sum function    var i,sum=0;      for (i=0;i<arguments.length;i++) {        sum+ =arguments[i]    ;    } return   sum;}) (1, 2, 3); // Output: 6

It is necessary to note that the JavaScript function does not check the type and number of parameters

Arguments Property callee (JavaScript 1.2 new property)

The callee property of the actual parameter object references the function that is currently executing. This is useful when the unnamed function calls itself recursively

(function(x) {// to find the factorial if of x    (typeof x = = "number" && x >0  {        return x * Arguments.callee (x-1);    }     return 1; // when x equals 0 o'clock Output 1}) (5); // Output:

Arguments Property Caller

The caller property of the actual parameter object refers to the calling environment in which the current function is called (that is , to return a reference to a function (f), which invokes the current function ).

  Note:Arguments.caller refers not to the function that called the current function, but rather to the actual parameter object of the function that called the current function. So to reference the calling function, you must use the FunctionName.caller.callee

But in the actual implementation of JavaScript, it refers directly to the calling function, not to the actual parameter object that called the function.

(function() {    function  inner () {        return  inner.caller    }    return  inner ();}) (); /* output: function () {    function inner () {        return Inner.caller    }    return inner ();}
According to everyone, this output is the original function of the anti-compilation text
This caller is not defined in the context of the definition, but is generated dynamically in the actual call.
function inner () {    return  F.caller;} (() {    return  inner ();}) (); // output: function () {//        return inner (); //      }
*/
It is necessary to note that if the current function is a top-level function, Functionname.caller returns null

  wonder: What is the actual use of this caller??? also hope that there are practical cases of friends can leave a message to tell me

 (see online parlance:Caller's application scenario is primarily used to view which function calls the function itself.) But I still did not think, what to support the situation need to see itself is called by that function, but in the "JavaScript authoritative guide" gives an example, apply the caller property to write a debug function, to output a stack of traces [function of tracking stack])

How to get the name of a function

//defining a function method for functions xxx () {}varReg =/function * (\w*) \ (\w*\)/;functionTest () {};varFunname =test.tostring (). Match (reg);if(funname) {Console.log (funname[1]);//Output: Test}//for var xxx = function () {} definition MethodvarTest =function(){};
var funname = test.tostring (). Match (reg);if(Funname && funname[1] = = "") {Console.log ("This function is an anonymous function");}

JavaScript function actual parameter object (arguments)/callee Property/Caller Property/method of recursive call/Get function name

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.