Javascript: passing multiple arguments as a single variable

Is it possible to pass multiple arguments using a single variable? For example, if I wanted to do something like:

function foo(x,y){ document.write("X is " + x); document.write("Y is " + y); } var bar = "0,10"; foo(bar); 

The above example is a simplified example of what I was trying to do. This does not work (because the "bar" is defined as a single argument). I know that there are simpler ways to implement this with arrays.

So, I ask this question mainly out of curiosity - is it possible for the variable "bar" to be detected as not one, but 2 arguments?

Thanks!

+7
javascript argument-passing
source share
10 answers
 function foo(thing) { document.write("X is " + thing.x); document.write("Y is " + thing.y); } var bar = {x:0, y:10}; foo(bar); 
+11
source share

What you ask for is impossible. If you want to pass multiple values โ€‹โ€‹in a single argument, use Array or Object . If you really have to use a string, you need to call split() to split the argument string into an array.

+4
source share
 function Add (a, b, c) { return a + b + c; } var nums = [1, 2, 4]; var sum = Add.apply (null, nums); 

variable length argument list:

 function Add () { var sum = 0; for (var i = 0; i < arguments.length; i++) { sum += arguments[i]; } return sum; } var n = Add (1, 2, 3, 4, 5); 

Link: apply a method (function object)

+3
source share

Not really.

You can do:

 window.foo.apply(window, bar.split(',')); 

(Apply allows you to pass an array of arguments instead of each argument separately)

... but the phrase ugly comes to mind.

+1
source share

You can use this:

 var bar = [0,10]; // creates an array foo(bar); function foo(arg){ document.write("X is " + arg[0]); document.write("Y is " + arg[1]); } 
0
source share

No, but you can pass an array or object:

 function foo(options){ document.write("X is " + options.x); document.write("Y is " + options.y); } var bar = {x: 0, y:10}; 
0
source share

No, It is Immpossible. You can put two arguments into an array, but the array is still one variable. Then you will need to rewrite the function to accept one variable, and consider it as an array, for example:

 function foo(x){ document.write("X is " + x[0]); document.write("Y is " + x[1]); } 

Basically, a function accepts variables as arguments, and no matter what variable it passes, each variable remains only one variable โ€” there is no way to get one variable that will be recognized as multiple arguments. An array is one variable, a JSON object is one variable, etc. These things have several parts, but they are encapsulated by a single variable.

0
source share

Of course, this is common for passing an object to parameters

 function foo(options){ //... } 

then you can convey anything ...

 var opts = {};//create an object opts['x'] = 5;//set whatever properties you want opts['y'] = 23; opts['border'] = 3; foo(opts);//pass 1 argument, with as many values as you want 

Often they are defined in a string, especially if values โ€‹โ€‹are not needed outside the method call.

 foo({'x':5,'y':23,'border':3}); 
0
source share

To answer your question, no. It is worth noting that the way you place bar defined only one value, a string containing "0.10".

-one
source share
 function myFunction(a,b){ //do stuff with a and b here } myFunction(1,'text') 

or...

 <a onClick="myFunction(1,'text');" 

There is an article about the problem here .

-2
source share

All Articles