我试图在JavaScript中返回两个值。这可能吗?
var newCodes = function() {
var dCodes = fg.codecsCodes.rs;
var dCodes2 = fg.codecsCodes2.rs;
return dCodes, dCodes2;
};
我试图在JavaScript中返回两个值。这可能吗?
var newCodes = function() {
var dCodes = fg.codecsCodes.rs;
var dCodes2 = fg.codecsCodes2.rs;
return dCodes, dCodes2;
};
当前回答
Ecmascript 6包括“解构赋值”(正如kangax提到的),因此在所有浏览器(不仅仅是Firefox)中,您将能够捕获值的数组,而不必为捕获它们的唯一目的而创建命名数组或对象。
//so to capture from this function
function myfunction()
{
var n=0;var s=1;var w=2;var e=3;
return [n,s,w,e];
}
//instead of having to make a named array or object like this
var IexistJusttoCapture = new Array();
IexistJusttoCapture = myfunction();
north=IexistJusttoCapture[0];
south=IexistJusttoCapture[1];
west=IexistJusttoCapture[2];
east=IexistJusttoCapture[3];
//you'll be able to just do this
[north, south, west, east] = myfunction();
你已经可以在Firefox中尝试了!
其他回答
除了像其他人推荐的那样返回一个数组或对象,你还可以使用一个收集器函数(类似于在the Little Schemer中找到的那个):
function a(collector){
collector(12,13);
}
var x,y;
a(function(a,b){
x=a;
y=b;
});
我做了一个jsperf测试,看看这三个方法中哪一个更快。数组是最快的,收集器是最慢的。
http://jsperf.com/returning-multiple-values-2
只返回一个对象文字
function newCodes(){
var dCodes = fg.codecsCodes.rs; // Linked ICDs
var dCodes2 = fg.codecsCodes2.rs; //Linked CPTs
return {
dCodes: dCodes,
dCodes2: dCodes2
};
}
var result = newCodes();
alert(result.dCodes);
alert(result.dCodes2);
添加缺失的重要部分,使这个问题成为一个完整的资源,因为它会在搜索结果中出现。
对象解构
在对象解构中,你不一定需要使用与你的变量名相同的键值,你可以通过定义一个不同的变量名,如下所示:
const newCodes = () => {
let dCodes = fg.codecsCodes.rs;
let dCodes2 = fg.codecsCodes2.rs;
return { dCodes, dCodes2 };
};
//destructuring
let { dCodes: code1, dCodes2: code2 } = newCodes();
//now it can be accessed by code1 & code2
console.log(code1, code2);
数组解构
在数组解构中,可以跳过不需要的值。
const newCodes = () => {
//...
return [ dCodes, dCodes2, dCodes3 ];
};
let [ code1, code2 ] = newCodes(); //first two items
let [ code1, ,code3 ] = newCodes(); //skip middle item, get first & last
let [ ,, code3 ] = newCodes(); //skip first two items, get last
let [ code1, ...rest ] = newCodes(); //first item, and others as an array
值得注意的是……Rest应该总是在末尾,因为在其他所有东西都聚合到Rest之后销毁任何东西没有任何意义。
我希望这将为这个问题增加一些价值:)
另一个值得一提的是新引入的(ES6)语法,除了析构赋值,还使用了对象创建简写。
function fun1() {
var x = 'a';
var y = 'b';
return { x, y, z: 'c' };
// literally means { x: x, y: y, z: 'c' };
}
var { z, x, y } = fun1(); // order or full presence is not really important
// literally means var r = fun1(), x = r.x, y = r.y, z = r.z;
console.log(x, y, z);
这个语法可以用babel或其他旧浏览器的js polyfiller填充,但幸运的是,现在本机适用于最新版本的Chrome和Firefox。
但是在创建一个新对象时,这里涉及到内存分配(以及最终的gc负载),因此不要期望从它那里获得太多性能。JavaScript不是开发高度优化的东西的最佳语言,但如果需要,你可以考虑将结果放在周围的对象上,或者是JavaScript、Java和其他语言之间常见的性能技巧。
在javascript中返回多个值的一个非常常见的方法是使用一个对象字面量,所以像这样:
const myFunction = () => {
const firstName = "Alireza",
familyName = "Dezfoolian",
age = 35;
return { firstName, familyName, age};
}
然后得到这样的值:
myFunction().firstName; //Alireza
myFunction().familyName; //Dezfoolian
myFunction().age; //age
或者更短的方式:
const {firstName, familyName, age} = myFunction();
把它们分开,比如:
firstName; //Alireza
familyName; //Dezfoolian
age; //35