22

Possible Duplicate:
Is it possible to send a variable number of arguments to a JavaScript function?

I can use arguments to get a variable number of arguments within a function, but how can I pass them to another function without knowing its prototype?

function show(foo, bar) { window.alert(foo+' '+bar); }
function run(f) { f(arguments); } // not correct, what to do?
run(show, 'foo', 'bar');

Note: I cannot guarantee the number of arguments needed for the function f that is passed to run. Meaning, even though the example shown has 2 arguments, it could be 0-infinite, so the following isn't appropriate:

function run(f) { f(arguments[1], arguments[2]); }
Community
  • 1
  • 1
steveo225
  • 11,394
  • 16
  • 62
  • 114
  • I disagree, as you can see, `show` has defined arguments, it does not use `arguments`. I have already tried `apply` that way with no luck. – steveo225 Sep 04 '11 at 17:57
  • Well, as you have noticed by now, `apply` is the only way to do this, therefore it *is* a duplicate. – Felix Kling Sep 04 '11 at 19:30
  • Yes, except the way `arguments` used was entirely different and ultimately the point of the question. – steveo225 Sep 04 '11 at 21:32

4 Answers4

30

The main way to pass a programmatically generated set of arguments to a function is by using the function's 'apply' method.

function show(foo, bar) {
  window.alert(foo+' '+bar);
}
function run(f) {
  // use splice to get all the arguments after 'f'
  var args = Array.prototype.splice.call(arguments, 1);
  f.apply(null, args);
}

run(show, 'foo', 'bar');
loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • For Posterity, if the function is an object method and needs to keep it's context, pass the `this` object instead of `null` with `f.apply(this, args);`. – tmthydvnprt Feb 12 '16 at 12:42
6

You can in fact do this with apply, if I understand your question correctly:

function show(foo, bar) { window.alert(foo+' '+bar); }
function run(f, args) { f.apply(null,args); } 
run(show, ['foo', 'bar']);
spike
  • 9,794
  • 9
  • 54
  • 85
  • The problem with that, I already have a framework setup using `arguments` and don't want to switch all the calls to use arrays. On that same not, I can try converting `arguments` from an object to an array and pass that. – steveo225 Sep 04 '11 at 18:09
  • Take a look at the solution from @Logan then, seems like it will do what you need. – spike Sep 04 '11 at 18:10
2

you need to use the apply function.. here is how u do it:

function variableFunction1()  
    {  

   alert("variableFunction1 arguments length: " + arguments.length);  

   // calls second varargs function keeping current 'this'.  
   variableFunction2.apply(this, arguments);  
}  

function variableFunction2()  
{  

   alert("variableFunction2 arguments length: " + arguments.length);  
}  

variableFunction1('a','b','c');  

Demo

Baz1nga
  • 15,485
  • 3
  • 35
  • 61
0

In your example to pass variable arguments to show this works

function show(foo, bar) { window.alert(foo+' '+bar); }
function run(f) { f.apply(null, Array().slice.call(arguments, 1)); }
run(show, 'foo', 'bar');  
James Kyburz
  • 13,775
  • 1
  • 32
  • 33