返回多个值并访问它们? [英] Return multiple values and access them?

查看:52
本文介绍了返回多个值并访问它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何构造它以返回多个值(消息和名称),并能够在js.html文件中访问它们?

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);
}

我已经解决了问题.谢谢大家的帮助和信息.这就是我所做的更改: http://pastebin.com/Ci1e8ZWx

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

推荐答案

一个函数只能返回一个值.

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;
}

使用结果:

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, accompanying_message: "some message"};
    return results;
}

使用:

var results = returnSomeValsAsObj();
console.log(results.primary_result);
console.log(results.secondary_result);
console.log(results.accompanying_message);

这篇关于返回多个值并访问它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆