Javascript function details 2 -- arguments

Source: Internet
Author: User

Javascript function details 2 -- arguments


Today we will continue with the topic of javascript Functions in the previous article. Today we will talk about arguments, a very important attribute of the function pair.

Related reading: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release/K/c/release + release/K/c/release + q94jMgLS0gdGhpc7bUz/release + release/K/c/release/ztcTE2rK/uq/K/Qo8YnI + release/ oyzsr9yv3X6dL908M8L3A + CjxwPmFyZ3VtZW50c8rHuq/K/bbUz/PE2rK/0ru49rHIvc/M2MritcTA4Mr91 + m2 1 M/zo6zV4rj2ttTP89bQsPy6rMHLy/queues/Lqs0ru49ta4z/K6r8r91K3QzbbUz/queues/y/u908rVtcTKx9K7uPayzsr9 Yv3X6aOs0rK + release/J0tTU2sn5w/release/aGjPC9wPgo8cD7I58/CtPrC68q + release "brush: java;"> function sayHi () {var name = arguments [0] | "default name", greeting = arguments [1] | "how are you"; console. log ("Hello" + name + "," + greeting);} // call sayHi () without passing a parameter; // call sayHi ("John") by passing a parameter "); // Call sayHi ("John", "Hello! ");

Execution result:


Next we will look at another piece of code to explore the reference relationship between the arguments object and the parameter list.

    function dosum(op1,op2){    arguments[1] = 20;    console.log(arguments[0]+op2);    }    dosum(1,1);

Let's first predict the execution result, 2 or 21?

The actual execution result is as follows:


For the above execution results, we can abstract the reference management model between the argument and the parameter array.


Arguments callee attribute

The arguments callee attribute is a pointer to a function object. flexible use of this attribute can greatly reduce the coupling between code and function names, and make code more flexible.

Reducing coupling is generally obvious in recursive operations.

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 with the function name factorial. As we mentioned at the beginning, the function name is a pointer, that is, a variable, if we can ensure that this variable always references this function object, this code will always be able to return the expected results. Once the variable points to another object, the above Code will go wrong, and arguments. callee will be able to play a role. Modify as follows:

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

In this way, the calculation result of this function object has nothing to do with the function name factorial. Even if we do the following, we can still get the correct result.

Var afunc = factorial;

Factorial = null;

Afunc (3); // 6 is returned correctly.

Another flexible application using arguments. callee is as follows:

    var otherfunc = (function(){    console.log("do something cool");    return arguments.callee;    })();

The operation in the code above immediately executes a function, returns a reference to the anonymous function, and assigns the value to otherfunc. Then we can continue to call this function elsewhere.

This method is used in projects. Some operations are required during page initialization, that is, the function is executed immediately after the page is loaded. However, this function is not a one-time function, we also need to call this function at other times on the page. The code above is more elegant than declaring a function first.

Original article: javascript function details 2 -- arguments

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.