How to create a Javascript function that accepts an arbitrary number of arguments?

Asked

Viewed 17,410 times

11

Functions of the type console.log take any number of arguments. How to specify this for a Javascript function?

3 answers

13


In Javascript, every function, regardless of the arguments specified in its signature, has an object arguments. This object contains all arguments passed to the function. But at first it is of type object, so it’s common to do this to turn you into a guy Array:

function minhaFuncao() {
  var args = [].slice.call(arguments, 0);
  for (var i = 0; i < args.length; i++) console.log(args[i]);
}

minhaFuncao(1, 2, 3); // args = [1, 2, 3];

The code above calls the function slice of prototype of Array in arguments, returning a Array so we can use your objects more easily.

In cases when you want to have a number of fixed arguments, and then variable arguments, just change the number passed as the second argument to slice.call:

function minhaFuncao(x, y) {
  var args = [].slice.call(arguments, 2);
  for (var i = 0; i < args.length; i++) console.log(args[i]);
}

minhaFuncao(1, 2, 3, 4, 5); // x = 1, y = 2, args = [3, 4, 5];

5

A more advisable approach is to pass an argument map:

function DoSomething(config) {
   config = config || {};
   var idioma = config.Idioma || "en-GB";
}

The first line initializes an empty map if the function has been called without arguments: DoSomething();

The function can be called like this:

DoSomething({ idioma : "pt-PT", outraChave: "outro valor" });

This approach has 2 advantages:

  • One can assign names to arguments, making it easier to maintain code over time;
  • How the processing depends on the name of the arguments, and not of its order, we can simply omit values - instead of calling the function with null:

    For example

    Let’s say the function expects 4 arguments (arg1, arg2, arg3, arg4), instead of:

    DoSomething(null, null, null, "ola");
    

    We would use:

    DoSomething({arg4: "ola"});
    

Several methods of jQuery libraries use this approach to pass a set of configuration data.

  • It’s a good alternative, but for a different case. Using a object allows you to specify "options", or named parameters, while the arguments is basically syntactic sugar to pass an array.

  • 1

    I agree. As the question did not specify the use case, I decided to suggest this option. Both have its use. arguments and' the most suitable to implement the example given in the question (console.log), in other cases it would be better to have a statement of arguments.

4

An example:

function somar() {
    var resultado = 0;

    for (var i = 0, len = arguments.length; i < len; i++) {
        resultado += arguments[i];
    }
    return resultado;
}

console.log(somar(1, 1, 1, 3, 3, 1, 5, 8, 7)); // = 30

console.log(somar(1, 1, 1)); // = 3

That is, just use the special variable arguments for each argument passed to the function. Being arguments[0] the first argument, arguments[1] the 2nd argument and so on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.