javascript 在javascript中返回多个值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4937665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:25:05  来源:igfitidea点击:

Returning multiple values in javascript?

javascript

提问by dexter

Is there a way to use a sort of the C#-like outor refparameter modifiers with Javascript to do something like this:

有没有办法在Javascript 中使用一种类似 C# 的outref参数修饰符来做这样的事情:

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.

我也想更改变量的引用。所以是的,通过引用传递参数。

回答by Tomasz Nurkiewicz

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

And you don't need xand yparameters, simply call:

而且你不需要xy参数,只需调用:

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

回答by John K.

You can create an "object"...

您可以创建一个“对象”...

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

return objOut;
}

回答by g.d.d.c

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

回答by Wayne

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

在 JavaScript 中有多种返回多个值的方法。您总是能够在数组中返回多个值:

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 (e.g. 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)

回答by meder omuraliev

You can return non-primitive types:

您可以返回非原始类型:

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

var x = blah();