简体   繁体   English

在javascript中返回多个值?

[英]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: 有没有办法使用一种类似C#的outref参数修饰符与Javascript来做这样的事情:

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: 而且您不需要xy参数,只需调用:

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: 如果在外部全局范围内定义'out'变量,如果你的函数可以在函数内部重新分配,如下所示:

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. 有几种方法可以在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)): 但是在JavaScript 1.7中添加了解构赋值的语法要好得多(如果你很幸运能够瞄准一个保证支持它的环境(例如Firefox扩展)):

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 ) 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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