views:

80

answers:

9

Hi, 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 example above is an simplified example of what I was trying to do. It doesn't work (because the "bar" is detected as a single argument). I know that there are easier ways to implement this using arrays.

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

Thanks!

A: 

You may 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]);
}
Topera
+5  A: 
function foo(thing) {
    document.write("X is " + thing.x);
    document.write("Y is " + thing.y);
}

var bar = {x:0, y:10};
foo(bar);
jtbandes
+3  A: 

What you're asking for is impossible. If you want to pass multiple values in a single argument, use an Array or an Object. If you really must use a string, you'll have to call split() to break the argument string into an array.

JSBangs
A: 

No, but you could pass a 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};
prodigitalson
+1  A: 

Not really.

You could do:

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

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

… but the phrase "ugly" comes to mind.

David Dorward
Yes the splitting of the string is ugly, also refering to `foo` using `window.foo` is ugly. However the use of `function.apply` is not!
adamse
A: 

No, it's not possible. You could put two arguments in an array, but an array is still one variable. Then you would need to rewrite the function to accept one variable, and treat it as an array, like this:

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 kind of variable you pass it, each variable is still only one variable - there's no way to get a single variable to be recognized as multiple arguments. An array is one variable, a JSON object is one variable, etc. These things have multiple parts to them, but they're encapsulated by a single variable.

Alex Zylman
A: 

sure, this is common to pass an object for options

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

then you can pass in anything...

var opts = {};
opts['x'] = 5;
opts['y'] = 23;
opts['border'] = 3;
foo(opts);
scunliffe
A: 

To directly answer your question, no. It's worth noting that the way you have bar defined it's only one value, a string containing "0,10".

Robert
A: 
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);

Reference: apply method (Function object)

gumape