[英]How can I return two parameters from a javascript function?
I have the following: 我有以下几点:
function getPk(entity) {
var store = window.localStorage;
switch (entity) {
case "City":
if (store.getItem('AccountID')) {
// Need to return both of the below pieces of information
return store.getItem('AccountID') + "04" + "000";
return "CityTable";
} else {
paramOnFailure("Please reselect");
return false;
}
break;
The problem is I need to be able to call this function and return two strings. 问题是我需要能够调用此函数并返回两个字符串。 Here I show two return statements but I know I cannot do that.
在这里,我显示了两个return语句,但我知道我不能这样做。
Is there a clean way I can return two strings to the function that calls my getPk(entity) function? 有没有一种干净的方法可以将两个字符串返回给调用getPk(entity)函数的函数?
If possible can you give an example also of how I can read what is returned. 如果可能,您还可以举一个例子,说明我如何阅读返回的内容。
Return them as either an array, or within an object. 以数组或对象的形式返回它们。
return [store.getItem('AccountID') + "04" + "000", "CityTable"];
or 要么
return { accountID: store.getItem('AccountID') + "04" + "000", table: "CityTable" };
You can only return a single value from a function in JavaScript, but that value can be a structure that contains multiple values internally, such as an array: 您只能从JavaScript中的函数返回单个值,但是该值可以是内部包含多个值的结构,例如数组:
return [store.getItem('AccountID') + "04" + "000", "CityTable"];
Just make sure that the functions which call this know about the conventions you're using for your return value. 只需确保调用此函数的函数了解您用于返回值的约定。
In javascript and like almost every languages, It is impossible to return more than one value. 在javascript中,几乎像每种语言一样,不可能返回多个值。
However, you can return a custom object containing all the values you want to return. 但是,您可以返回包含所有要返回的值的自定义对象。
See the following link to find how to create an object. 请参阅以下链接以查找如何创建对象。 Create an object with properties,
创建具有属性的对象,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.