Return multiple variables from a JavaScript function?

后端 未结 2 1406
囚心锁ツ
囚心锁ツ 2021-02-01 06:10

In python, to return multiple variables, I can do --

def function_one(i):
    return int(i), int(i) * 2

value, duble_value = function_one(1)

H

相关标签:
2条回答
  • 2021-02-01 06:22

    You need to either use an array or an object.

    For example:

    function test() {
        return {foo: "bar", baz: "bof"};
    }
    
    function test2() {
        return ["bar", "bof"];
    }
    
    var data = test();
    foo = data.foo;
    baz = data.baz;
    
    data = test2();
    foo = data[0];
    baz = data[1];
    
    0 讨论(0)
  • 2021-02-01 06:26
    function foo(){
        return ["something","something else","something more","something further"];
    }
    
    let [a,b,c,d] = foo();
    
    0 讨论(0)
提交回复
热议问题