Jump to content

The Return Statement


iwato
 Share

Recommended Posts

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

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

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

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

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

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

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

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

The quotes aren't necessary
This 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.) Edited by Deirdre's Dad
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...