JavaScript function 2--arguments

Source: Internet
Author: User

Today we go on to the next article to continue the topic of JavaScript functions. What we're going to talk about today is the--arguments of a function to an important attribute in the image.

Related reading:

  • JavaScript function 1--Overview
  • JavaScript function 2--arguments
  • Javascript function 3--This object
  • Javascript function 4--other properties of the function
  • Javascript functions 5--intrinsic functions of function objects

Arguments object parameter Array reference

Arguments is a special class array object inside a function object that contains a list of the parameters of the owning function, along with a pointer to the function prototype object.

Let's discuss the parameter array in arguments, and we can access the parameters directly using [index] in a similar way to accessing the array. To really understand this feature, let's start by knowing that JavaScript's function doesn't care about the arguments you pass, because when you call a function, inside the function, he receives an array of arguments, that is, theoretically, we can declare the function without declaring any arguments, However, it is possible to pass any number of arguments at the time of invocation.

The following code example:

    function Sayhi () {    var name = arguments[0]| | Default name ",    = arguments[1]| |" How IS is You ";    Console.log ("Hello" +name+ "," + greeting);    }     // do not pass parameter calls     Sayhi ();     // pass a parameter call    Sayhi ("John");     // two parameter calls    Sayhi ("John", "Hello!" ");

Execution Result:

Next we look at another piece of code to explore the reference relationship of the arguments object to the argument list.

    function Dosum (OP1,OP2) {    arguments[1] =;    Console.log (arguments[0]+op2);    }    Dosum (a);

Let's start by predicting the results, 2 or 21?

The actual results of the implementation are as follows:

We can abstract the reference management model between argument and the parameter array for the above execution results.

Arguments callee Properties

The Arguments callee property is a pointer to a function object that can be used flexibly to greatly reduce the coupling between the code and the function name, while making the code more flexible.

The reduction of coupling degree is generally obvious in recursive operation.

The following code:

    function factorial (num) {    if(num<=1) {    return 1;     Else {    return num*factorial (num-1);    }    }

The biggest drawback of the above code is the coupling to the function name factorial, as we started by saying, it is a pointer, a variable, and if we can guarantee that the variable will always refer to the function object, then this code will always return the result we expect. Once the variable points to another object, the code above will go wrong, and Arguments.callee will be able to work. Modify the following:

    function factorial (num) {    if(num <= 1) {    return 1;    } Else {    return num * Arguments.callee (num-1 );    }    }

In this way, the result of this function object is not related to the name factorial, even if we do the following, we can still get the correct result.

var afunc = factorial;

factorial = null;

Afunc (3); The same can be returned correctly 6

Another more flexible application through Arguments.callee is as follows:

    var otherfunc = (function() {    console.log ("Do something Cool");     return Arguments.callee;    }) ();

The operation of the above code immediately executes a function and returns a reference to the anonymous function, assigned to Otherfunc, where we can continue to invoke the function elsewhere.

This usage is what I encountered in the project, the page initialization to do something, that is, when the page is loaded to execute the function immediately, but this function is not a one-time, we also need to call this function at other times of the page, the above code, the individual will be more than the first declaration of a function, And then after the page loading is finished, call to be elegant, a matter of opinion.

Text from: JavaScript function 2--arguments

JavaScript function 2--arguments

Related Article

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.