Return multiple values in JavaScript?

前端 未结 20 2002
暖寄归人
暖寄归人 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:06

    You can also do:

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

    Well we can not exactly do what your trying. But something likely to below can be done.

    function multiReturnValues(){
        return {x:10,y:20};
    }
    

    Then when calling the method

    const {x,y} = multiReturnValues();
    
    console.log(x) ---> 10
    console.log(y) ---> 20
    
    0 讨论(0)
  • 2020-11-22 14:08

    Since ES6 you can do this

    let newCodes = function() {  
        const dCodes = fg.codecsCodes.rs
        const dCodes2 = fg.codecsCodes2.rs
        return {dCodes, dCodes2}
    };
    
    let {dCodes, dCodes2} = newCodes()
    

    Return expression {dCodes, dCodes2} is property value shorthand and is equivalent to this {dCodes: dCodes, dCodes2: dCodes2}.

    This assignment on last line is called object destructing assignment. It extracts property value of an object and assigns it to variable of same name. If you'd like to assign return values to variables of different name you could do it like this let {dCodes: x, dCodes2: y} = newCodes()

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

    I would suggest to use the latest destructuring assignment (But make sure it's supported in your environment)

    var newCodes = function () {
        var dCodes = fg.codecsCodes.rs;
        var dCodes2 = fg.codecsCodes2.rs;
        return {firstCodes: dCodes, secondCodes: dCodes2};
    };
    var {firstCodes, secondCodes} = newCodes()
    
    0 讨论(0)
  • 2020-11-22 14:09

    I know of two ways to do this: 1. Return as Array 2. Return as Object

    Here's an example I found:

    <script>
    // Defining function
    function divideNumbers(dividend, divisor){
        var quotient = dividend / divisor;
        var arr = [dividend, divisor, quotient];
        return arr;
    }
    
    // Store returned value in a variable
    var all = divideNumbers(10, 2);
    
    // Displaying individual values
    alert(all[0]); // 0utputs: 10
    alert(all[1]); // 0utputs: 2
    alert(all[2]); // 0utputs: 5
    </script>
    
    
    
    <script>
    // Defining function
    function divideNumbers(dividend, divisor){
        var quotient = dividend / divisor;
        var obj = {
            dividend: dividend,
            divisor: divisor,
            quotient: quotient
        };
        return obj;
    }
    
    // Store returned value in a variable
    var all = divideNumbers(10, 2);
    
    // Displaying individual values
    alert(all.dividend); // 0utputs: 10
    alert(all.divisor); // 0utputs: 2
    alert(all.quotient); // 0utputs: 5
    </script>
    
    0 讨论(0)
  • 2020-11-22 14:10

    Another worth to mention newly introduced (ES6) syntax is use of object creation shorthand in addition to destructing assignment.

    function fun1() {
      var x = 'a';
      var y = 'b';
      return { x, y, z: 'c' };
      // literally means { x: x, y: y, z: 'c' };
    }
    
    var { z, x, y } = fun1(); // order or full presence is not really important
    // literally means var r = fun1(), x = r.x, y = r.y, z = r.z;
    console.log(x, y, z);
    

    This syntax can be polyfilled with babel or other js polyfiller for older browsers but fortunately now works natively with the recent versions of Chrome and Firefox.

    But as making a new object, memory allocation (and eventual gc load) are involved here, don't expect much performance from it. JavaScript is not best language for developing highly optimal things anyways but if that is needed, you can consider putting your result on surrounding object or such techniques which are usually common performance tricks between JavaScript, Java and other languages.

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