In this article I’ll show you how to handle multiple parameters – the equivalent to
A Pattern For Using Multiple Arguments In Functions Today
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Or with as many arguments you want:
1 2 3 4 5 6 7 8
Or even with more arguments than the parameters defined in the function:
It is up to you to implement the body of a function and handle each case as you see fit. But what happens when you want to write a function that takes an arbitrary number of arguments? In C# whenever we want to write such a function we use the
The only approach possible is to use the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
There are a couple of specially interesting things to point out in this example.
The first one is the fact that we are sending arguments of different types to the function and they are all treated seamlessly. That’s an example of duck typing in action where an object is defined by what it can do and not by what the object type is. As long as the values that we pass as arguments support the interface the function implementation expects – in this case the
toString method – everything will work just fine.
The second thing to point out is the use of the
Array.prototype.slice function to convert the
arguments variable into a real array
victims. While you could think that
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
But that does not have any of the array functions that you would expect:
1 2 3 4 5 6 7 8 9
slice function of
Array.prototype allows you to convert
arguments to an array and take advantage of all the nice array functions. This is what we did with the
obliterate function within the first example:
1 2 3 4 5 6 7 8
Native Multiple Arguments with ECMASCript 2015 With Rest Parameters
All the complexities of using the
arguments object in the previous examples can be substitued by rest parameters and handled in a much seamless fashion:
1 2 3 4 5 6
When using the rest parameters syntax, the
victims parameter becomes an array so there’s no need to perform additional conversions.
You can test the example above on this jsFiddle snippet.
params in C#.
A Sidenote on Using Array.prototype.slice On Arguments
While I was writing this article I found out that there’s a drawback when using
Array.prototype.slice to convert
The recommended approach is to use
Array.slice generic method – Array generic methods are specially implemented to be used on other types – but it is not a part of the ECMAScript specification (not even of ES6/2015) and is only implemented in certain browsers (try testing
Array.slice in the web developer console on Chrome, Firefox and Internet Explorer and you’ll see that it only exists in Firefox).
You have two options:
- use a polyfill that falls back to
Array.prototype.slicewhen there are no generic functions available
- ignore the recommendation based on the fact that functions with an arbitrary of arguments are uncommon :)
This problem will take care of itself when we start using the rest parameter syntax.
More Articles in These Series
- Object Oriented Programming
- White Tower Summoning Enhanced: The Marvels of ES6 Classes
- Black Tower Summoning: Object Composition with Mixins
- Data Structures
- Functional Programming
- And more stuff to be shaped out:
- Asynchronous Programming