[thelist] JS Boolean Object...
James Aylard
evolt at pixelwright.com
Fri Dec 5 10:16:49 CST 2003
jsWalter wrote:
>>> function someFunc(myVar)
>>> {
>>> if ("Blue" == myVar)
>>> {
>>> return true ;
>>> }
>>> return false ;
>>> }
>
> BTW: After reading this message again, it finally hit me, you have 2
> exit points in your function.
>
> Me, I'm a stickler for the old mind set - "One way in - one way out"
Well, if you only want one way out, that is easily resolved by assigning
a value to a variable and returning that -- although unnecessary, IMO. BTW,
the simplest way to write the function that I offered would be:
function someFunc(myVar)
{
return("Blue" == myVar) ;
}
But I see, re-reading your original post, that you did ask for more than
just a Boolean value. To me, the simplest way to do this would be to create
a variable for holding an error value, and to set that variable within the
someFunc() funtion (which I've adjusted to have "one way out"), e.g.:
var strErr, bolValue, someVar ;
someVar = "Blue" ;
strErr = "" ; // default value
bolValue = someFunc(someVar) ;
if (bolValue)
{
// do something here
}
else
{
alert(strErr) ;
}
function someFunc(myVar)
{
var blnReturn = false ;
if ("Blue" == myVar)
{
blnReturn = true ;
strErr = "" ;
}
else
{
strErr = "It's not Blue" ;
}
return blnReturn ;
}
You could also compact the first part slightly by writing it as:
if(someFunc(someVar))
But that only makes sense if you're not going to re-use bolValue
anywhere else. Either way, this example is nothing fancy, but if you're
wanting to avoid confusing others who have to read your code, I would guess
that this is the easiest way to keep everyone sane.
> function someFunc(myVar)
> {
> bolVal = true ;
>
> if ("Blue" == myVar)
> return true ;
>
> return bolVal ;
> }
But this function will _always_ return true. What's the point of that?
Just a coding slip, I assume? And you have two return statements, which
suggests _two_ ways out, not one. I don't get it. Did you mean:
function someFunc(myVar)
{
bolVal = false ;
if ("Blue" == myVar)
{
bolVal = true ;
}
return bolVal ;
}
Is that it?
James Aylard
More information about the thelist
mailing list