Monday, May 7, 2012

An Equivalent To C# params In JavaScript: Accepting Arguments And Arrays

Accepting arguments and arrays

In programming, frequently you'll come across a situation where you want to create a method or function that accepts an unspecified number of arguments. What is also common in this scenario is that you would sometimes have an array you would like to pass to said method or function. Coming from a .NET background, I would have tackled this with C#'s params keyword, but in JavaScript things are a little different.

JavaScript's arguments collection

When writing a function in JavaScript, while in the function body you have access to a collection called arguments. The arguments collection itself is not an array, it's an Arguments object. It's very similar to an array, but it's a different animal. The arguments object is simple enough though, it has a length property, and you can iterate through the arguments passed to a method by index. A good example of a function that might make use of the arguments collection is a concat function like so:


function concat() {
    var s = '';
    for(var i = 0; i < arguments.length; i++) {
     s += arguments[i];   

The function above can take any number of arguments and concatenate them together, then alert them. This works really well when we are calling it like so:


concat('blah',{}); //blah[object Object]

But when we have a bunch of data in arrays, it seems like we're SOL unless we do something hokey like use eval('') to pass the arguments:


concat([1,2,3,'foo']); //1,2,3,foo ... not what we expected.

C# params keyword

In C# we had the params keyword, which allows us to pass arguments one at a time, or all together as an array. Here is an example of the same basic function as a C# method.


public void Concat(params object[] args) {
   var s = new StringBuilder();
   foreach(var arg in args) {

The method above can bee called in the following ways:


//multiple arguments
Concat(1, 2, 3, 4, 5);

//or an entire array at once
var numbers = new[] { 1, 2, 3, 4, 5 };

JavaScript's apply() method

In JavaScript there is a little known method called apply() we can leverage to pass an array to a function as arguments. The following example shows how apply() is used in this scenario:


var numbers = [1,2,3,4,5];
concat.apply(window, numbers);

In the code above, you can see the apply method being called. The first argument is the scope the apply method should act in. The second argument is, of course, the array we want to pass to the function.

Other uses for JavaScript's apply() method

There are also native JavaScript methods with which we can make use of the apply method to great effect. Two that I can think of off the top of my head are Math.min and Math.max.


var numbers = [1,2,3,4,5];
Math.min.apply(Math, numbers); //1
Math.max.apply(Math, numbers); //5

No comments:

Post a Comment

This form allows some basic HTML. It will only create links if you wrap the URL in an anchor tag (Sorry, it's the Blogger default)