javascript 在一个函数中返回两个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19738207/
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
Return two variables in one function
提问by Adam Mo.
Consider the following code (demo):
考虑以下代码(演示):
function test(){
var h = 'Hello';
var w = 'World';
return (h, w);
}
var test = test();
alert(test);
On execution the function test
only returns the second value (i.e. 'World'
). How do I make it return multiple values?
执行时,该函数test
仅返回第二个值(即'World'
)。如何让它返回多个值?
回答by SnoringFrog
You cannot explicitly return two variables from a single function, but there are various ways you could concatenate the two variables in order to return them.
您不能从单个函数显式返回两个变量,但是可以通过多种方式连接两个变量以返回它们。
If you don't need to keep the variables separated, you could just concatenate them directly like this:
如果您不需要将变量分开,您可以像这样直接连接它们:
function test(){
var h = 'Hello';
var w = 'World';
var hw = h+w
return (hw);
}
var test = test();
alert(test);
This would alert "HelloWorld". (If you wanted a space in there, you should use var hw = h+" "+w
instead.
这会提醒“HelloWorld”。(如果你想要一个空间,你应该使用var hw = h+" "+w
。
If you need to keep the two variables separated, you can place them into an array like so:
如果需要将两个变量分开,可以将它们放入数组中,如下所示:
function test(){
var h = "Hello";
var w = "World";
var hw=[h,w];
return hw;
}
var test = test();
alert(test);
This allows the h
and w
values to still be accessed individually as test[0]
and test[1]
, respectively. However, alert(test) here will display "Hello,World" because of the way alert() handles arrays (that is, it prints a comma-separated list of each element in the array sequentially). If you wanted to produce the same output as your example code, you would need use something like join()
. join()
will construct a string from an array, it takes one argument which serves as a separator between the elements. To reproduce the two alerts from my first example, you would need to use alert(test.join(""))
and alert(test.join(" ")
, respectively.
这允许h
和w
值仍然分别作为test[0]
和单独访问test[1]
。但是,由于 alert() 处理数组的方式(也就是说,它按顺序打印数组中每个元素的逗号分隔列表),这里的 alert(test) 将显示“Hello,World”。如果您想生成与示例代码相同的输出,则需要使用类似join()
. join()
将从数组构造一个字符串,它需要一个参数作为元素之间的分隔符。要重现我的第一个示例中的两个警报,您需要分别使用alert(test.join(""))
和alert(test.join(" ")
。
My example could be shortened slightly by skipping the creation of the hw
variable and just returning an array directly. In that case, test() would look like this:
通过跳过hw
变量的创建并直接返回一个数组,可以稍微缩短我的示例。在这种情况下, test() 看起来像这样:
function test(){
var h="Hello";
var w="World";
return [h, w];
}
This could also be done as an object with return { h : h, w : w };
, in which case you would access the individual variables as test.h and test.w, respectively.
这也可以作为带有 的对象来完成return { h : h, w : w };
,在这种情况下,您可以分别以 test.h 和 test.w 的形式访问各个变量。
回答by Vicky Gonsalves
function test(){
var h = 'Hello';
var w = 'World';
return {h:h,w:w}
}
var test = test();
alert(test.h);
alert(test.w);
one simple way to do is to return Object containing multiple key value pairs.
一种简单的方法是返回包含多个键值对的对象。
回答by nnnnnn
The comma operatorevaluates each operand and then returns the value of the last one.
该逗号操作评估每个操作数,然后返回最后一个的值。
You'd need to either return an array:
您需要返回一个数组:
return [h, w];
...or an object:
...或一个对象:
return { h : h, w : w };
Which you'd then use as:
然后您将其用作:
var test = test();
alert(test[0]); // "hello" - in the case of the array version
...or:
...或者:
var test = test();
alert(test.w); // "world" in the case of the object version
回答by Daniel A. White
You can return an array or a new object.
您可以返回一个数组或一个新对象。