iwato Posted May 1, 2009 Share Posted May 1, 2009 QUESTION: How does one get the return statement to return more than one value?BACKGROUND: The following code works great up to the point where I try to return the values of both bName and bVersion. Here is what I have tried:return bName bVersion; // Returns an errorreturn bName, bVersion; // Returns only bVersionUnfortunately, I need the values of both bName and bVersion, and I need them separately. Must I create an object? function getVersionNameAndNumber() { var patt1 = /\S+\/\S+/g; var bNV_String = navigator.userAgent; var bNV_Matches = new Array(); var bNV = new Array(); bNV_Matches = bNV_String.match(patt1); for (var i=0; i<bNV_Matches.length; i++) { bNV_Match = bNV_Matches[i]; bNV = bNV_Match.split("/"); var bName = bNV[0] var bVersion = bNV[1]; if (bName == "Firefox" || bName == "Opera") { return bName, bVersion; } else if (bName == "Safari") { for (var j=0; j<bNV_Matches.length; j++) { bNV_AppleMatch = bNV_Matches[j]; bNV_Apple = bNV_AppleMatch.split("/"); var bName_Apple = bNV_Apple[0]; var bVersion_Apple = bNV_Apple[1]; if (bName_Apple == "Version") { bVersion = bVersion_Apple; return bName, bVersion; } } } else {} }}document.write(getVersionNameAndNumber()); Link to comment Share on other sites More sharing options...
jeffman Posted May 1, 2009 Share Posted May 1, 2009 You could return an array object or a complex object. (There's not much difference in Javascript.) The different values might be more readable if the members have names, so maybe an associative array or a more complex object. But it's not a big deal, like it would be in C where you'd have to handle your own garbage collection. Link to comment Share on other sites More sharing options...
jesh Posted May 1, 2009 Share Posted May 1, 2009 This is a perfect use for JSON. You could have a return statement in your function like so: function getVersionNameAndNumber(){ ... return {"Name" : bName, "Version" : bVersion};} And then use it like this: var results = getVersionNameAndNumber();alert(results.Version);// or// alert(results["Version"]); Link to comment Share on other sites More sharing options...
jeffman Posted May 1, 2009 Share Posted May 1, 2009 I actually tried that out in my error console, just to be sure, and it worked fine, but then I couldn't remember if IE supported it, and finding out would have meant going into the other room, and that would have meant standing up . . . you see where this is going? Link to comment Share on other sites More sharing options...
jesh Posted May 1, 2009 Share Posted May 1, 2009 I would have tested earlier, but getting coffee was the more important task at hand. Now that I'm satiated, the following works just fine in IE6: function test(){ return {"Name":"jesh", "Version":1};}var obj = test();alert(obj.Name);alert(obj["Version"]); Link to comment Share on other sites More sharing options...
iwato Posted May 1, 2009 Author Share Posted May 1, 2009 You could return an array object or a complex object.I was able to create an object -- perhaps what you call a complex object -- using the following construction:return bNV_Object = {name:bName, version:bVersion};This made it very easy to extract bName and bVersion later on.Thank you for responding.Roddy Link to comment Share on other sites More sharing options...
iwato Posted May 1, 2009 Author Share Posted May 1, 2009 function test() { return {"Name":"jesh", "Version":1};}var obj = test();alert(obj.Name);alert(obj["Version"]); Are the quotation marks necessary, though? I did it without, and everything appears to work just fine. I can see now what is meant when Deirdre's Dad wrote that the object and the array are treated very similarly. Thank you for the additional input.Roddy Link to comment Share on other sites More sharing options...
justsomeguy Posted May 1, 2009 Share Posted May 1, 2009 The quotes aren't necessary, this type of object notation is used extensively in ExtJS and it works in all browsers. Link to comment Share on other sites More sharing options...
jeffman Posted May 1, 2009 Share Posted May 1, 2009 Javascript treats an array like an object. If you use typeof on an array, it returns "object". So I feel funny about writing things like "you could return an array or an object" because I figure some wiseguy will point out that an array is an object. (Yeah yeah yeah.)Example: o={}; o['tim'] = 5; // o.tim returns 5a=[];a['tim'] = 5;// a.tim returns 5 For that matter, a string is a Javascript object too, and you can even assign properties to a string. (I'm not sure what the cross-browser support is for that, either, though.) Link to comment Share on other sites More sharing options...
jeffman Posted May 1, 2009 Share Posted May 1, 2009 The quotes aren't necessaryThis fact may not seem important in this context, but when you send AJAX data in the form of JSON strings, every character you cut out contributes to speed.(Though I should add that PHP is picky about quote marks in JSON.) Link to comment Share on other sites More sharing options...
justsomeguy Posted May 1, 2009 Share Posted May 1, 2009 Yeah, other languages processing JSON might have issues with no quotes, but Javascript itself doesn't need them. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.