Return multiple values in JavaScript?

前端 未结 20 1988
暖寄归人
暖寄归人 2020-11-22 13:17

I am trying to return two values in JavaScript. Is this possible?

var newCodes = function() {  
    var dCodes = fg.codecsCodes.rs;
    va         


        
相关标签:
20条回答
  • 2020-11-22 14:11

    Other than returning an array or an object as others have recommended, you can also use a collector function (similar to the one found in The Little Schemer):

    function a(collector){
      collector(12,13);
    }
    
    var x,y;
    a(function(a,b){
      x=a;
      y=b;
    });
    

    I made a jsperf test to see which one of the three methods is faster. Array is fastest and collector is slowest.

    http://jsperf.com/returning-multiple-values-2

    0 讨论(0)
  • 2020-11-22 14:13

    Best way for this is

    function a(){
         var d=2;
         var c=3;
         var f=4;
         return {d:d,c:c,f:f}
    }
    

    Then use

    a().f
    

    return 4

    in ES6 you can use this code

    function a(){
          var d=2;
          var c=3;
          var f=4;
          return {d,c,f}
    }
    
    0 讨论(0)
  • 2020-11-22 14:14

    Just return an object literal

    function newCodes(){
        var dCodes = fg.codecsCodes.rs; // Linked ICDs  
        var dCodes2 = fg.codecsCodes2.rs; //Linked CPTs       
        return {
            dCodes: dCodes, 
            dCodes2: dCodes2
        };  
    }
    
    
    var result = newCodes();
    alert(result.dCodes);
    alert(result.dCodes2);
    
    0 讨论(0)
  • 2020-11-22 14:14

    You can use "Object"

    function newCodes(){
        var obj= new Object();
        obj.dCodes = fg.codecsCodes.rs;
        obj.dCodes2 = fg.codecsCodes2.rs;
    
        return obj;
    }
    
    0 讨论(0)
  • 2020-11-22 14:15

    You can do this from ECMAScript 6 onwards using arrays and "destructuring assignments". Note that these are not available in older Javascript versions (meaning — neither with ECMAScript 3rd nor 5th editions).

    It allows you to assign to 1+ variables simultaneously:

    var [x, y] = [1, 2];
    x; // 1
    y; // 2
    
    // or
    
    [x, y] = (function(){ return [3, 4]; })();
    x; // 3
    y; // 4
    

    You can also use object destructuring combined with property value shorthand to name the return values in an object and pick out the ones you want:

    let {baz, foo} = (function(){ return {foo: 3, bar: 500, baz: 40} })();
    baz; // 40
    foo; // 3
    

    And by the way, don't be fooled by the fact that ECMAScript allows you to return 1, 2, .... What really happens there is not what might seem. An expression in return statement — 1, 2, 3 — is nothing but a comma operator applied to numeric literals (1 , 2, and 3) sequentially, which eventually evaluates to the value of its last expression — 3. That's why return 1, 2, 3 is functionally identical to nothing more but return 3.

    return 1, 2, 3;
    // becomes
    return 2, 3;
    // becomes
    return 3;
    
    0 讨论(0)
  • 2020-11-22 14:16

    Adding the missing important parts to make this question a complete resource, as this comes up in search results.

    Object Destructuring

    In object destructuring, you don't necessarily need to use the same key value as your variable name, you can assign a different variable name by defining it as below:

    const newCodes = () => {  
        let dCodes = fg.codecsCodes.rs;
        let dCodes2 = fg.codecsCodes2.rs;
        return { dCodes, dCodes2 };
    };
    
    //destructuring
    let { dCodes: code1, dCodes2: code2 } = newCodes();
    
    //now it can be accessed by code1 & code2
    console.log(code1, code2);
    

    Array Destructuring

    In array destructuring, you can skip the values you don't need.

    const newCodes = () => {  
        //...
        return [ dCodes, dCodes2, dCodes3 ];
    };
    
    let [ code1, code2 ] = newCodes(); //first two items
    let [ code1, ,code3 ] = newCodes(); //skip middle item, get first & last
    let [ ,, code3 ] = newCodes(); //skip first two items, get last
    let [ code1, ...rest ] = newCodes(); //first item, and others as an array
    

    It's worth noticing that ...rest should always be at the end as it doesn't make any sense to destruct anything after everything else is aggregated to rest.

    I hope this will add some value to this question :)

    0 讨论(0)
提交回复
热议问题