我试图在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中尝试了!
其他回答
你可以从ECMAScript 6开始使用数组和“解构赋值”来做到这一点。请注意,这些在旧的Javascript版本中是不可用的(也就是说,ECMAScript第三版和第五版都不可用)。
它允许你同时赋值给1+个变量:
var [x, y] = [1, 2];
x; // 1
y; // 2
// or
[x, y] = (function(){ return [3, 4]; })();
x; // 3
y; // 4
你也可以使用对象解构结合属性值简写来命名对象中的返回值,并挑出你想要的:
let {baz, foo} = (function(){ return {foo: 3, bar: 500, baz: 40} })();
baz; // 40
foo; // 3
顺便说一下,不要被ECMAScript允许返回1,2,....这一事实所愚弄那里真正发生的不是表面上看起来的那样。return语句中的表达式- 1,2,3 -只是一个逗号操作符,它依次应用于数值字面值(1,2,3),最终计算为其最后一个表达式- 3的值。这就是为什么return 1,2,3在功能上和return 3是一样的。
return 1, 2, 3;
// becomes
return 2, 3;
// becomes
return 3;
添加缺失的重要部分,使这个问题成为一个完整的资源,因为它会在搜索结果中出现。
对象解构
在对象解构中,你不一定需要使用与你的变量名相同的键值,你可以通过定义一个不同的变量名,如下所示:
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之后销毁任何东西没有任何意义。
我希望这将为这个问题增加一些价值:)
都是正确的。Return逻辑地从左到右处理并返回最后一个值。
function foo(){
return 1,2,3;
}
>> foo()
>> 3
我建议使用最新的解构赋值(但要确保它在您的环境中得到支持)
var newCodes = function () {
var dCodes = fg.codecsCodes.rs;
var dCodes2 = fg.codecsCodes2.rs;
return {firstCodes: dCodes, secondCodes: dCodes2};
};
var {firstCodes, secondCodes} = newCodes()
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中尝试了!