简体   繁体   中英

Returning multiple values in javascript?

Is there a way to use a sort of the C#-like out or ref parameter modifiers with Javascript to do something like this:

function myManyReturnFunction(number1, number2, out x, out y) {
    x = number1 * number2;
    y = number1 / number2;

    return true;
}

var height1, height2 = 0;
var check = myManyReturnFunction(1,1, out height1, out hight2);

I would like to change the variable's reference as well. So yes, passing an argument by reference.

function myManyReturnFunction(number1, number2) {
    return {
        x: number1 * number2,
        y: number1 / number2
    }
}

And you don't need x and y parameters, simply call:

var result = myManyReturnFunction(6, 9);
var x = result.x;
var y = result.y;

You can create an "object"...

function getObj(){
var objOut = new Object();
objOut.name = "Smith";
objOut.State = "Arkansas";

return objOut;
}

If you define the 'out' variables in the global scope outside if your function they can be reassigned inside the function with something like this:

var height1 = 0, height2 = 0;

function myManyReturnFunction(number1, number2) {
    height1 = number1 * number2;
    height2 = number1 / number2;

    return true;
}


var check = myManyReturnFunction(1,1);

You can also create an object, which you can then pass to your function, which can be modified within the function like so:

var myValues = {}

function setValues(num1, num2, vals) {
  vals.x = num1 * num2;
  vals.y = num1 / num2;

  return True;
}

setValues(1, 1, myValues);

There are several ways to return multiple values in JavaScript. You've always been able to return multiple values in an array:

function f() {
    return [1, 2];
}

And access them like this:

var ret = f();
document.write(ret[0]); // first return value

But the syntax is much nicer in JavaScript 1.7 with the addition of destructuring assignment (if you're lucky enough to be targeting an environment guaranteed to support it (eg a Firefox extension)):

var a, b;
[a, b] = f();
document.write("a is " + a + " b is " + b + "<br>\n");

Another option is to return an object literal containing your values:

function f() {
    return { one: 1, two: 2 };
}

Which can then be accessed by name:

var ret = f();
document.write(ret.one + ", " + ret.two);

And of course you could do something really horrible like modify global scope or even set properties on the function itself:

function f() {
    f.one = 1;
    f.two = 2;
}

f();

document.write(f.one + ", " + f.two);

More reading (and the source of some of these examples):

https://developer.mozilla.org/en/New_in_JavaScript_1.7#Destructuring_assignment_(Merge_into_own_page.2fsection )

You can return non-primitive types:

function blah() {
  return { name: 'john', age: 23 };
};

var x = blah();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM