Effective JavaScript Item 22 uses arguments to create a function that accepts a mutable argument list

Source: Internet
Author: User

this series as effective JavaScript 's reading notes.

in the Item , the paper introduces a combination of Apply variable argument list function implemented by method Average , it actually declares only an array as a parameter, but uses the Apply method, you can actually accept several elements as parameters:

function Averageofarray (a) {for (var i = 0, sum = 0, n = a.length; i < n; i++) {sum + = A[i];} return sum/n;} Averageofarray.apply (null, [1, 2, 3, 4, 5]);

and using arguments variable, you can also remove the declared parameters. That is, a function can not explicitly declare any parameters. the arguments object provides an array-like usage: it can be accessed using an index, and it has a length property to indicate how many elements it contains, so The above functions can be implemented like this:


function average () {for (var i = 0, sum = 0, n = arguments.length; i < n; i++) {sum + = arguments[i];} return sum/n;}

the above declaration method allows Average functions are more flexible, but when working with array parameters, you need to combine Apply method, because Apply method to "break" the array parameters into a single element, and then these elements form the arguments variable.

One rule of thumb is: when you provide the use arguments variable, it also provides a fixed-length version of the parameter, because the former can always take advantage of the latter:


function average () {return averageofarray (arguments);}

in this way, users can not use the Apply method, call the function that you provided. Because when you use the apply method, you lose a portion of the code readability and run performance.

Summarize:

    1. Use an implicit arguments object to implement a function that accepts a variable-length argument list
    2. In addition to the variable-length parametric function provided, a function of the fixed lengths parameter is also provided to avoid using Apply Method


Effective JavaScript Item 22 uses arguments to create a function that accepts a mutable argument list

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.