return 从函数返回到调用的多个值

return multiple value from function back to call

我是菜鸟...而且我确信对于我要问的问题(回调?)有更好的术语,但基本上我想学习如何将函数中计算的值返回到从哪里调用函数。例如:

$(function() {
    setValues();
    console.log(val1, val2, val3);

});

function setValues() {
    var val1 = 10;
    var val2 = 20;
    var val3 = 30;  
}

尝试这样的事情:

$(function() {
    var myvalues = setValues(); //this stores the values you got from the function

   //now you can do something like console.log(myvalues.value1) to get the first value


});

function setValues() {
    var val1 = 10;
    var val2 = 20;
    var val3 = 30;  
    return {value1: val1, value2: val2, value3: val3} //this sends the data back to the original place where you called the function
}

我建议阅读 this article on object literals