40

I have a function that accepts any number and kind of arguments, so no specific parameter has been defined. This function should call another function passing all arguments.

The problem is that I can pass "arguments" in order to include all arguments but in this case it will work like a single argument and not the way we expect arguments to work.

An example:

The main function:

function handleCall() {
   // let's call a sub-function
   // and pass all arguments (my question is how this is handled the right way)
   function callSubFunction( arguments );
}

function callSubfunction( userid, customerid, param) {
   // passed arguments are now 
   alert( 'userid = ' + userid );

   // this will not work, you have to use arguments[2]
   alert( param );
  }

The example call:

handleCall( 1029, 232, 'param01' );

Using the approach above, all arguments will be stored in "userid" as pseudo-array and items can be accessed e.g. arguments[2] but not using the parameter name "param".

In ColdFusion, the solution for such stuff is the parameter "argumentCollection", this way you can pass parameters stored in a structure without being converted to a single argument with the type struct containing all key/values.

How can I achieve the same with JavaScript?

Naveed
  • 41,517
  • 32
  • 98
  • 131
Hansjoerg
  • 403
  • 1
  • 4
  • 4

3 Answers3

78

If you want to do the same with the spread syntax, you can use the following:

function handleCall(...args) {
    callSubFunction(...args);
}
alexmngn
  • 9,107
  • 19
  • 70
  • 130
48

You can use the .apply() method to call a function and pass the arguments as a set.

callSubFunction.apply( this, arguments ); 

The first argument will set the value of this in the allSubFunction method. I just set it to the current this value. The second is the collection of arguments to send.

So your handleCall() function will look like:

function handleCall() {
     //set the value of "this" and pass on the arguments object
    callSubFunction.apply( this, arguments );
}

It isn't required that you send an Arguments object. You could send an Array of arguments if the circumstance required.

Johann
  • 27,536
  • 39
  • 165
  • 279
user113716
  • 318,772
  • 63
  • 451
  • 440
  • how to do it in ES6 function? – IsmailS Jan 17 '20 at 14:45
  • 1
    The link to the documentation here is **dead**. [My edit](https://stackoverflow.com/suggested-edits/5041911), which correctly **fixed** the links, was rejected by two users with *the same* copy-pasted message about "readability", despite the message not affecting visible text. (I suspect they were farming/botting for site rep.) In the meantime, here is a working link to MDN's `Function.prototype.apply()`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply – JamesTheAwesomeDude Jan 27 '21 at 22:57
1

Use apply like so:

function Foo()
{
    Bar.apply(this, arguments);
}

function Bar(a, b)
{
    alert(a);
    alert(b);
}
Jimmy Chandra
  • 6,472
  • 4
  • 26
  • 38