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