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?
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.
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.
Parameters can be passed to a method in following three ways : Value Parameters. Reference Parameters. Output Parameters.
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.
If you want to do the same with the spread syntax, you can use the following:
function handleCall(...args) { callSubFunction(...args); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With