By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,600 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,600 IT Pros & Developers. It's quick & easy.

returning a list

P: n/a
I'd like to return two values from a function. I'm doing this:

function getTwo(){
// some code
return new Array(value_a,value_b);
}

var returnArray=getTwo(some_var);

var value_a=returnArray[0];
var value_b=returnArray[1];

Is there a cleaner way of doing that? I'm used to perl list assignments.

Cheers,
Jeff
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
JRS: In article <0s*****************@newsread1.news.atl.earthlink. net>,
seen in news:comp.lang.javascript, Jeff Thies <no****@nospam.net> posted
at Thu, 18 Dec 2003 15:10:52 :-
I'd like to return two values from a function. I'm doing this:

function getTwo(){
// some code
return new Array(value_a,value_b);
}

var returnArray=getTwo(some_var);

var value_a=returnArray[0];
var value_b=returnArray[1];

Is there a cleaner way of doing that? I'm used to perl list assignments.


Yes. You could have written return [value_a, value_b] } .

However, if the result is not naturally an array, consider

... return { A:value_a, B:value_b } }

then with (getTwo(some_var)) { var value_a = A, value_b = B }

or Ob = getTwo(some_var)
and then work with Ob.A & Ob.B .

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
Jul 20 '05 #2

P: n/a
> However, if the result is not naturally an array, consider

... return { A:value_a, B:value_b } }

or Ob = getTwo(some_var)
and then work with Ob.A & Ob.B .
That's really nice! I'm calling a function that calculates many different
properties. Now I can retrieve any of these that are in the hash. Perfect
for astronomical calculations.

BTW. Wanted to thank you for the javascript time/date FAQ. Used it a bunch
yesterday!

Cheers,
Jeff
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript <URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics,

links.
Jul 20 '05 #3

P: n/a
Jeff Thies wrote:
However, if the result is not naturally an array, consider

... return { A:value_a, B:value_b } }

or Ob = getTwo(some_var)
and then work with Ob.A & Ob.B .


That's really nice! I'm calling a function that calculates many different
properties. Now I can retrieve any of these that are in the hash. Perfect
for astronomical calculations.

BTW. Wanted to thank you for the javascript time/date FAQ. Used it a bunch
yesterday!

Cheers,
Jeff


If you know what you want to return, such as the description of a star, I would
strongly urge you to create an object Star, then return an instance of that
object. I don't know the properties of a star, but the code would look something
like:

function Star(distance, brightness, temperature) {
this.distance = distance;
this.brightness = brightness;
this.temperature = temperature;

this.setDistance = function(d) {
if (d > 1000000000) {
return -1;
} else {
this.distance = d;
return this.distance;
}
}
this.getDistance = function() {
return this.distance + 'au';
}
this.getBrightness = function() {
return this.brightness;
}
this.getTemperature = function() {
return this.temperature;
}
}

Then in your function that returns information about a star, it could do
something like:

return new Star(1, 2, 3);

And the code that uses the returned value would be:

var someStar = someFunctionThatReturnsAStarObject();
document.write('The star is ' + someStar.getDistance() + ' away');
if (someStar.setDistance(100000000000) == -1) {
document.write('Something went wrong setting the star\'s distance');
} else {
document.write('The star is now ' + someStar.getDistance() + ' away');
}

By creating a Star object, you can encapsulate data validation (as in the method
setDistance() and you manipulate the output in a single place (as with adding
'au' to the value returned by getDistance()).

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #4

P: n/a
> If you know what you want to return, such as the description of a star, I
would
strongly urge you to create an object Star, then return an instance of that object. I don't know the properties of a star, but the code would look something like:

function Star(distance, brightness, temperature) {
this.distance = distance;
this.brightness = brightness;
this.temperature = temperature;

this.setDistance = function(d) {
if (d > 1000000000) {
return -1;
} else {
this.distance = d;
return this.distance;
}
}
this.getDistance = function() {
return this.distance + 'au';
}
this.getBrightness = function() {
return this.brightness;
}
this.getTemperature = function() {
return this.temperature;
}
}
I thought of making this more object oriented. Usually when I call the
function I need 2 or 3 of the return values. These values just kind of fall
out as the calculation proceeds. ie: var a may take 100% of the
calculations, var b may take 80%. var c 98%. The calculations are extensive
and I didn't want to redo them.

Perhaps a wrapper around the function that turns it into an object???

Below is the Moon function:

function getMoon(date){
var lo=64.975464;
var Po=349.383063;
var No=151.950429;
var i=5.145396;

var sun=(findSun(date));
var LAMDAsun=sun.LAMDAsun;
var Msun=sun.M;
var D=dayDiff(date);
var l=(13.1763966*D)+lo;
l=setRange(l);
Mm=setRange(l - .1114041 * D - Po);
var N=setRange(No - .0529539 * D);
var C=l-LAMDAsun;
var Ev=1.2739 * Math.sin(degree(2*C - Mm));
var Ae=.1858 * Math.sin(degree(Msun));
var A3=.37 * Math.sin(degree(Msun));
var Mpm=Mm+Ev-Ae-A3;
var Ec=6.2886 * Math.sin(degree(Mpm));
var A4=.214 * Math.sin(degree(2* Mpm));
var lp=l+Ev+Ec-Ae+A4;
var V=.6583 * Math.sin(degree(lp-LAMDAsun)*2);
var lpp=lp+V;

var Np=N + (.16 * Math.sin(degree(LAMDAsun))); // test should be wrong

var y=Math.sin(degree(lpp-Np)) * Math.cos(degree(i));
var x=Math.cos(degree(lpp-Np));
var arctan=Math.atan(y/x)*180/Math.PI;

// arctan amiguity test
if((y>0)&&(x<0)){arctan=arctan+180};
if((y<0)&&(x<0)){arctan=arctan+180};
if((y<0)&&(x>0)){arctan=arctan+360};

var LAMDAm=arctan+Np;
var Bm=Math.asin(Math.sin(degree(lpp-Np))*Math.sin(degree(i)))*180/Math.PI;
var equatorial=eclipticToEquatorial(LAMDAm,Bm);
var Dm=lpp-LAMDAsun; // Dm is the phase of the moon in degrees ~13
degrees/day
var F=(1-Math.cos(degree(Dm)))/2;

return{F:F,Dm:Dm,alpha:equatorial.alpha,delta:equa torial.delta,B:Bm,LAMDA:LA
MDAm};
}

whew... Where do you draw the line between procedural, functional, object
oriented and functional retrieve "hash objects"?

maybe I should have gone to school after OO code was invented. No one ever
asks me to write fortran.

Cheers,
Jeff

Then in your function that returns information about a star, it could do
something like:

return new Star(1, 2, 3);

And the code that uses the returned value would be:

var someStar = someFunctionThatReturnsAStarObject();
document.write('The star is ' + someStar.getDistance() + ' away');
if (someStar.setDistance(100000000000) == -1) {
document.write('Something went wrong setting the star\'s distance');
} else {
document.write('The star is now ' + someStar.getDistance() + ' away');
}

By creating a Star object, you can encapsulate data validation (as in the method setDistance() and you manipulate the output in a single place (as with adding 'au' to the value returned by getDistance()).

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...3/reference/fr
ames.html
* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...l_reference_en
try.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.