Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript unknown number of arguments

in my project, I register different functions (having different number of arguments) as listeners to a number of events. When the event takes place, I need to fire the associated function. I receive the parameters to be passed to listener method in the form of an array whereas the listener function expect each separate argument. So, I am doing it like this but I do not like the approach and would like to know if there is an elegant way of doing it,

function callListenerWithArgs(func, args){
        switch(args.length){
            case 1:
                func(args[0]);
                break;
            case 2:
                func(args[0], args[1]);
                break;
            case 3:
                func(args[0], args[1], args[2]);
                break;
            case 4:
                func(args[0], args[1], args[2], args[3]);
                break;
            default:
                func();
        }
    }
like image 611
Nutty Avatar asked Jun 22 '11 15:06

Nutty


People also ask

What happens if you pass an extra parameter to a method in JavaScript?

JavaScript is extremely broad-minded about the number of arguments you pass to a function. If you pass too many, the extra ones are ignored. If you pass too few, the missing parameters get assigned the value undefined .

How do you write a function that accepts any number of arguments?

When you call a function in JavaScript, you can pass in any number of arguments, regardless of what the function declaration specifies. There is no function parameter limit. In the above function, if we pass any number of arguments, the result is always the same because it will take the first two parameters only.

How can you get the total number of arguments passed to a function in JavaScript?

length property provides the number of arguments actually passed to a function. This can be more or less than the defined parameter's count (see Function. length ).

Can a function have multiple parameters JavaScript?

Functions can accept more than one argument. When calling a function, you're able to pass multiple arguments to the function; each argument gets stored in a separate parameter and used as a discrete variable within the function.


1 Answers

Use .apply

func.apply(null, args)

If you need to bind to a specific scope, you can pass another argument in to use as this inside the function:

func.apply(scope, args);

Also, a nuance of JavaScript is that you can call functions with undefined values. So making a small tweak to your existing code will work in 95% of all cases (this isn't suggested as a solution, just pointing it out):

// will handle any number of args up to 7
function callListenerWithArgs(func, args){
    func(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
}

If your func is defined as:

function foo(a, b, c){
}

you get a, b, c passed in, along with some more undefined values that get ignored. As I said above, this works in 95% of cases. It doesn't work if you ever check arguments.length in the called function since it will always be the same, regardless of the number of parameters the function defines.

like image 161
Nobody Avatar answered Nov 15 '22 06:11

Nobody