我试图在JavaScript中返回两个值。这可能吗?

var newCodes = function() {  
    var dCodes = fg.codecsCodes.rs;
    var dCodes2 = fg.codecsCodes2.rs;
    return dCodes, dCodes2;
};

当前回答

几天前,我有类似的要求,从我创建的函数中获得多个返回值。

从许多返回值,我需要它只返回特定的值为一个给定的条件,然后其他返回值对应于其他条件。


以下是我如何做到这一点的例子:

功能:

function myTodayDate(){
    var today = new Date();
    var day = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    var month = ["January","February","March","April","May","June","July","August","September","October","November","December"];
    var myTodayObj = 
    {
        myDate : today.getDate(),
        myDay : day[today.getDay()],
        myMonth : month[today.getMonth()],
        year : today.getFullYear()
    }
    return myTodayObj;
}

从函数返回的对象获取所需的返回值:

var todayDate = myTodayDate().myDate;
var todayDay = myTodayDate().myDay;
var todayMonth = myTodayDate().myMonth;
var todayYear = myTodayDate().year;

回答这个问题的关键是分享以良好格式获取Date的方法。希望对你有所帮助:)

其他回答

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中尝试了!

添加缺失的重要部分,使这个问题成为一个完整的资源,因为它会在搜索结果中出现。

对象解构

在对象解构中,你不一定需要使用与你的变量名相同的键值,你可以通过定义一个不同的变量名,如下所示:

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之后销毁任何东西没有任何意义。

我希望这将为这个问题增加一些价值:)

使用模板字面量' ${}'可以返回一个包含许多值和变量的字符串

如:

var newCodes = function() {  
    var dCodes = fg.codecsCodes.rs;
    var dCodes2 = fg.codecsCodes2.rs;
    return `${dCodes}, ${dCodes2}`;
};

它既简短又简单。

只返回一个对象文字

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);
function a(){
  var d = 2;
  var c = 3;
  var f = 4;
  return {d: d, c: c, f: f};
}

然后使用

const {d, c, f} = a();

新版本:

function a(){
  var d = 2;
  var c = 3;
  var f = 4;
  return {d, c, f}
}