Item 22: Use arguments to Create Variadic Functions

Item 22: Use arguments to Create Variadic Functions
Item 21 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, but it does not define any
explicit formal parameters. Instead, it makes use of the fact that
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 over 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 have 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 way, consumers of your functions don’t have to resort to the
apply method, which can be 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 you provide so that your consumers don’t need to use the
apply method.

文章来源于:Effective+Javascript编写高质量JavaScript代码的68个有效方法 英文版

时间: 2024-08-29 02:58:39

Item 22: Use arguments to Create Variadic Functions的相关文章

Effective JavaScript Item 22 使用arguments来创建接受可变参数列表的函数

本系列作为Effective JavaScript的读书笔记. 在Item 21中,介绍了结合apply方法实现的可变参数列表函数average,它实际上只声明了一个数组作为参数,但是利用apply方法,实际上可以接受若干元素作为参数: function averageOfArray(a) { for (var i = 0, sum = 0, n = a.length; i < n; i++) { sum += a[i]; } return sum / n; } averageOfArray.a

Go by Example: Variadic Functions

可变参数函数支持任意数量的传入参数.例如:fmt.Println 就是一个常见的可变参数函数. package main import "fmt" // 这个函数可以使用任意数量的int型数作为参数 func sum(nums ...int) { fmt.Print(nums, " ") total := 0 for _, num := range nums { total += num } fmt.Println(total) } func main() { //

Use apply to Call Functions with Different

Item 21: Use apply to Call Functions with DifferentNumbers of ArgumentsImagine that someone provides us with a function that calculates theaverage of any number of values: average(1, 2, 3); // 2 average(1); // 1 average(3, 1, 4, 1, 5, 9, 2, 6, 5); //

Effective JavaScript Item 29 避免使用非规范的Stack Inspection属性

本系列作为Effective JavaScript的读书笔记. 由于历史原因,很多JavaScript执行环境中都提供了某些方式来查看函数调用栈.在一些环境中,arguments对象(关于该对象可以查看Item 22,23,24)上有两个额外的属性: arguments.callee - 它引用了正在被调用的函数 arguments.caller - 它引用了调用当前函数的函数 关于arguments.callee的使用,可以参考下面的代码: var factorial = (function(

OCP-1Z0-051-题目解析-第22题

22. You need to create a table for a banking application. One of the columns in the table has the following requirements: 1) You want a column in the table to store the duration of the credit period. 2) The data in the column should be stored in a fo

5.24 Declaring Attributes of Functions【转】

转自:https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html 5.24 Declaring Attributes of Functions In GNU C, you declare certain things about functions called in your program which help the compiler optimize function calls and check your

variadic function _ golang

Variadic functions can be called with any number of trailing arguments. For example, fmt.Println is a common variadic function package main import ( "fmt" ) func sum(nums ...int) { fmt.Println(nums, " ") total := 0 for _, num := range

Effective JavaScript Item 51 在类数组对象上重用数组方法

Array.prototype对象上的标准方法被设计为也可以在其它对象上重用 - 即使不是继承自Array的对象.因此,在JavaScript中存折一些类数组对象(Array-like Objects). 一个典型的例子是函数的arguments对象,在Item 22中对它进行过介绍.该对象并不继承自Array.prototype,所以我们不能直接调用arguments.forEach来对其中的元素进行遍历.但是,我们可以通过首先得到forEach方法的对象,然后调用call方法(可以参考Ite

Effective JavaScript Item 21 使用apply方法调用函数以传入可变参数列表

本系列作为Effective JavaScript的读书笔记. 下面是一个拥有可变参数列表的方法的典型例子: average(1, 2, 3); // 2 average(1); // 1 average(3, 1, 4, 1, 5, 9, 2, 6, 5); // 4 average(2, 7, 1, 8, 2, 8, 1, 8); // 4.625 而以下则是一个只接受一个数组作为参数的例子: averageOfArray([1, 2, 3]); // 2 averageOfArray([1