最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return multiple values and access them? - Stack Overflow

programmeradmin4浏览0评论

How would I structure this to return multiple values (message and name), and be able to access them in the js.html file?

 

--code.gs

function createArtistTable(name)
{
    var message = "test";

    //return message and name
}

--js.html

function openArtistTable(name)
{
    google.script.run
        .withSuccessHandler(openSuccess)
        .withFailureHandler(openFailure)
        .createArtistTable(name)
}

function openSuccess(//have 2 values here -- var1, var2)
{
    console.log(var1);
    console.log(var2);
}

EDIT:

I've fixed the problem. Thank you all for the help and information. This is what I changed:

How would I structure this to return multiple values (message and name), and be able to access them in the js.html file?

 

--code.gs

function createArtistTable(name)
{
    var message = "test";

    //return message and name
}

--js.html

function openArtistTable(name)
{
    google.script.run
        .withSuccessHandler(openSuccess)
        .withFailureHandler(openFailure)
        .createArtistTable(name)
}

function openSuccess(//have 2 values here -- var1, var2)
{
    console.log(var1);
    console.log(var2);
}

EDIT:

I've fixed the problem. Thank you all for the help and information. This is what I changed: http://pastebin./Ci1e8ZWx

Share Improve this question edited Sep 20, 2016 at 4:51 user4411473 asked Sep 20, 2016 at 2:59 user4411473user4411473 2091 gold badge4 silver badges10 bronze badges 6
  • 2 Either you can return it as an array or as an object. – Karpak Commented Sep 20, 2016 at 3:01
  • 1 return an object like {message: message, name: name} – CoderLim Commented Sep 20, 2016 at 3:03
  • @CoderGLM How would I access it in the openSuccess function? – user4411473 Commented Sep 20, 2016 at 3:07
  • call createArtistTable in openSuccess – CoderLim Commented Sep 20, 2016 at 3:09
  • @CoderGLM Something like var x = createArtistTable().message ? – user4411473 Commented Sep 20, 2016 at 3:10
 |  Show 1 more ment

1 Answer 1

Reset to default 5

A function can only return one value.

So the way to do this is to wrap them together inside an array or object.

function return2Vals()
{
    var var1;
    var var2;
    //Code that does stuff with var1 and var2
    ///
    ///
    //Create an array with the values and return it.
    var results = [var1, var2];
    return results;
}

Using the result:

var vals = return2Vals();
console.log("One of the return values is:", vals[0]);
console.log("The other return value is:", vals[1]);

Alternatively you could use an object and basically do whatever you want by using an object:

function returnSomeValsAsObj()
{
    var var1;
    var var2;
    //Code that does stuff with var1 and var2
    ///
    ///
    //Create an object with the values and return it.
    var results = {primary_result: var1, secondary_result: var2, acpanying_message: "some message"};
    return results;
}

Using:

var results = returnSomeValsAsObj();
console.log(results.primary_result);
console.log(results.secondary_result);
console.log(results.acpanying_message);
发布评论

评论列表(0)

  1. 暂无评论