Return multiple values in JavaScript?

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

    Ecmascript 6 includes "destructuring assignments" (as kangax mentioned) so in all browsers (not just Firefox) you'll be able to capture an array of values without having to make a named array or object for the sole purpose of capturing them.

    //so to capture from this function
    function myfunction()
    {
     var n=0;var s=1;var w=2;var e=3;
     return [n,s,w,e];
    }
    
    //instead of having to make a named array or object like this
    var IexistJusttoCapture = new Array();
    IexistJusttoCapture = myfunction();
    north=IexistJusttoCapture[0];
    south=IexistJusttoCapture[1];
    west=IexistJusttoCapture[2];
    east=IexistJusttoCapture[3];
    
    //you'll be able to just do this
    [north, south, west, east] = myfunction(); 
    

    You can try it out in Firefox already!

提交回复
热议问题