Jump to content
iwato

The Return Statement

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());

Share this post


Link to post
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.

Share this post


Link to post
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"]);

Share this post


Link to post
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?

Share this post


Link to post
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"]);

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

The quotes aren't necessary, this type of object notation is used extensively in ExtJS and it works in all browsers.

Share this post


Link to post
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.)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Yeah, other languages processing JSON might have issues with no quotes, but Javascript itself doesn't need them.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...