Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass all arguments as collection to another function and not as single argument? [duplicate]

Tags:

javascript

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?

like image 874
Hansjoerg Avatar asked Jan 16 '11 15:01

Hansjoerg


People also ask

How do you pass all arguments to a function?

Within any function, you can use the arguments variable to get an array-like list of all of the arguments passed into the function. You don't need to define it ahead of time. It's a native JavaScript object. You can access specific arguments by calling their index.

How do you assign a all the arguments to a single variable?

Assigning the arguments to a regular variable (as in args="$@" ) mashes all the arguments together like "$*" does. If you want to store the arguments in a variable, use an array with args=("$@") (the parentheses make it an array), and then reference them as e.g. "${args[0]}" etc.

What are the three ways to pass argument values to a function call?

Parameters can be passed to a method in following three ways : Value Parameters. Reference Parameters. Output Parameters.

What is the name of a special array that contains all the arguments passed to the function when it is called?

Original contents. The arguments object is a special construct available inside all function calls. It represents the list of arguments that were passed in when invoking the function. Since JavaScript allows functions to be called with any number args, we need a way to dynamically discover and access them.


1 Answers

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

function handleCall(...args) {     callSubFunction(...args); } 
like image 80
alexmngn Avatar answered Sep 26 '22 02:09

alexmngn