Item 22:use arguments to Create Variadic Functions

Source: Internet
Author: User
Tags variadic

Item 22:use arguments to Create Variadic Functions
Item describes a variadic average function, which can process an
Arbitrary number of arguments and produce their average value. How
Can we implement a variadic function of our own? The fixed-arity ver-
Sion, Averageofarray, is easy enough to implement:
function Averageofarray (a) {
for (var i = 0, sum = 0, n = a.length; i < n; i++) {
Sum + = A[i];
}
return sum/n;
}
Averageofarray ([2, 7, 1, 8, 2, 8, 1, 8]); 4.625
The definition of Averageofarray defines a single formal parameter, the
Variable A in the parameter list. When consumers call Averageofarray,
They provide a single argument (sometimes called an actual argu-
ment to distinguish it clearly from the formal parameter), the array of
Values.
The variadic version is almost identical and it does not define any
Explicit formal parameters. Instead, it makes use of the fact
JavaScript provides every function with an implicit local variable
Called arguments. The arguments object provides an Array-like interface
To the actual Arguments:it contains indexed properties for each actual
Argument and a length property indicating how many arguments were

provided. This makes the variable-arity average function expressible
By looping through each element of the arguments object:
function average () {
for (var i = 0, sum = 0, n = arguments.length;
I < n;
i++) {
Sum + = Arguments[i];
}
return sum/n;
}
Variadic functions make for flexible interfaces; Different clients can
Call them with different numbers of arguments. But by themselves,
They also lose a bit of convenience:if consumers want to call them
With a computed array of arguments, they has to use the Apply
Method described in Item 21. A good rule of thumb is that whenever
You provide a variable-arity function for convenience, you should also
Provide a fixed-arity version that takes an explicit array. This is usu-
Ally easy to provide, because you can typically implement the Variadic
function as a small wrapper that delegates to the fixed-arity version:
function average () {
return Averageofarray (arguments);
}
This, consumers of your functions don ' t has to resort to the
Apply method, which can is less readable and often carries a perfor-
Mance cost.
Things to Remember
? Use the implicit arguments object to implement Variable-arity
Functions.
? Consider providing additional fixed-arity versions of the Variadic
Functions provide so, your consumers don ' t need to use the
Apply method.

Article from: Effective+javascript 68 effective ways to write high-quality JavaScript code English version

Item 22:use arguments to Create Variadic Functions

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.