从函数返回多个值返回调用

时间:2015-03-17 20:52:10

标签: jquery function callback

我是一个noobie ......而且我确信有更好的术语可以解释我的问题(回调?)但基本上我想学习如何将函数中的值计算回到函数被调用。 E.g:

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

});

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

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

$(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

相关问题