1

In function .concat(), I can pass an arbitrary number of arguments to it.

I understand function overloading in C++, but I don't know how implement a function with unknown number of arguments in JavaScript.

How do I implement an arbitrary number of arguments to a function?

2
  • can you elaborate your question? Commented Jul 9, 2011 at 4:38
  • Sigh, I just wasted first post on correcting spelling mistakes in the post, and now Blender even got rid of that edit. Not a good start on a Saturday morning... Commented Jul 9, 2011 at 4:47

6 Answers 6

1

In javascript, you would use the built in parameter called "arguments" which is an array of all the arguments passed to the function. You can obtain it's length with arguments.length and each value from the array arguments[0], arguments[1], etc... Every function has this built in variable that you can use.

For example, a function to concatenate all strings passed to it.

function concatAll() {
    var str;
    for (var i = 0 ; i < arguments.length; i++) {
        str += arguments[i];
    }
    return(str);
}

var f = concatAll("abc", "def", "ghi");   // "abcdefghi"
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this using the arguments object. See the examples and documentation here: https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments

Comments

1

Like this -

function f()
{
    var i;

    for(i=0; i<arguments.length; i++)
    {
        alert( (i+1) +  "th argument: " + arguments[i]);
    }
}

All the functions in javascript has a built-in parameter called arguments which is an array containing all the function arguments passed to the function. Just iterate over this array and you will be able to access all the arguments of a function.

As an example, once I've written a function which is used to enable/disable certain button if some specific fields were not empty. I wrote this function this way -

function toggleButton()        // I used jquery inside this function
{
    var i;
    var last = arguments.length-1;

    for(i=0; i<last; i++)
    {
        if( $.trim($(arguments[i]).val()) === "" )
            return false;
    }

    $(arguments[last]).toggle();

    return true;
}

and called this function like this -

toggleButton("#idOfFirstField", "#idOfSecondField", "#idOfButtonToToggle");

or like this -

toggleButton("#idOfFirstField", "#idOfSecondField", "#idOfThirdField", "#idOfButtonToToggle");

so in both the cases, I was passing variable number of field ids to the function and it checked that if these fields were empty. If all of them contained some value, then it toggled the visibility of the button.

Comments

0

Like this - use the arguments object all functions have available :

function someFunction() {
  for (var i=0,n=arguments.length;i<n;i++) {
    // do something with arguments[i];
  }
}

Comments

0

You can use the arguments array to access parameters that are not formally declared inside the function:

function printArguments() {
  for (i = 0; i < printArguments.arguments.length; i++)
    document.writeln(printArguments.arguments[i] + '<br />');
}

printArguments(1, 2, 3, 'etc');

Source: http://www.irt.org/articles/js008/

1 Comment

Haha - you are SO late, but at least it is a post from the site I used to be JS editor at. Good to see that code from 1997 still works
0

Any javascript function can have an arbitrary number of arguments. If function execution depends on the number or specific qualities of it's arguments, you'll have to check the arguments object, which can be iterated like an 'Arraylike' object, as others have shown.

In some cases it may be handy to convert the arguments to a real array, using something like
var args = Array.prototoype.slice(arguments).

Here's a blog entry from John Resigs page on method overloading that may interest you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.