Use apply to Call Functions with Different
Item 21: Use apply to Call Functions with Different
Numbers of Arguments
Imagine that someone provides us with a function that calculates the
average of any number of values:
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
The average function is an example of what’s known as a variadic or
variable-arity function (the arity of a function is the number of argu-
ments it expects): It can take any number of arguments. By com-
parison, a fixed-arity version of average would probably take a single
argument containing an array of values:
1 averageOfArray([1, 2, 3]); // 2 2 averageOfArray([1]); // 1 3 averageOfArray([3, 1, 4, 1, 5, 9, 2, 6, 5]); // 4 4 averageOfArray([2, 7, 1, 8, 2, 8, 1, 8]); // 4.625
The variadic version is more concise and arguably more elegant. Vari-
adic functions have convenient syntax, at least when the caller knows
ahead of time exactly how many arguments to provide, as in the
examples above. But imagine that we have an array of values:
var scores = getAllScores();
How can we use the average function to compute their average?
average(/* ? */);
Fortunately, functions come with a built-in apply method, which is
similar to their call method, but designed just for this purpose. The
apply method takes an array of arguments and calls the function as
if each element of the array were an individual argument of the call.
In addition to the array of arguments, the apply method takes a first
argument that specifies the binding of this for the function being
called. Since the average function does not refer to this , we can sim-
ply pass it null :
var scores = getAllScores(); average.apply(null, scores);
If scores turns out to have, say, three elements, this will behave the
same as if we had written:
average(scores[0], scores[1], scores[2]);
The apply method can be used on variadic methods, too. For example,
a buffer object might contain a variadic append method for adding
entries to its internal state (see Item 22 to understand the implemen-
tation of append ):
var buffer = {
state: [], append: function() { for (var i = 0, n = arguments.length; i < n; i++) { this.state.push(arguments[i]); } } };
The append method can be called with any number of arguments:
buffer.append("Hello, "); buffer.append(firstName, " ", lastName, "!"); buffer.append(newline);
With the this argument of apply , we can also call append with a com-
puted array:
buffer.append.apply(buffer, getInputStrings());
Notice the importance of the buffer argument: If we passed a dif-
ferent object, the append method would attempt to modify the state
property of the wrong object.
Things to Remember
✦ Use the apply method to call variadic functions with a computed
array of arguments.
✦ Use the first argument of apply to provide a receiver for variadic
methods.
文章来源于:Effective+Javascript编写高质量JavaScript代码的68个有效方法 英文版