[thelist] JS Boolean Object...

Jeff Howden jeff at jeffhowden.com
Thu Dec 4 21:59:51 CST 2003


walter,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: jsWalter
>
> > in your case, "if(bolValue)" becomes a check for
> > existence, which will return true.
>
> That is not correct, according to my experiments...
>
>     bolValue = someFunc ( 7 );
>
>     if ( bolValue )  // true
>         alert ( 'yes' );
>     else
>         alert ( 'no' );
>
>     function someFunc ( x )
>     {
>        y = ( x == 6 );   // y is a boolean
>        return y
>     }
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

read what i wrote again along with what sam-i-am wrote.

any variable that exists and does not have a null, undefined, or false value
will return true.

so, of bolValue has a boolean value of true, "if(bolValue)" will return
true.  likewise, if bolValue is an object (so you can add the "err"
property) it will also return true.

your experiment is flawed in that you're simply checking against a boolean
value.  try checking that same variable when it's an object and you'll get
the same results.

    bolValue = someFunc ( 7 );

    if ( bolValue )  // true
        alert ( 'yes' );
    else
        alert ( 'no' );

    function someFunc ( x )
    {
       y = new Object();
       y.err = 'foo is bar';
       return y;
    }

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> > if(bolValue && !bolValue.err) // true
> >   // do something here
> > else
> >   alert(bolValue.err);
>
> yes, that would work, but it does not follow "standard"
> boolean logic flow.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

sure it does.  it says, in plain english, if bolValue is true (exists and
does not have a value of null, undefined, or false) and it either does not
have a property named "err" or it has an "err" property with a value of
null, undefined, or false, then the entire statement is true.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> I didn't want to make this rocket science, or something
> special that a developer had to learn in order to use
> my class methods.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

agreed.  however, by creating a variable with a boolean value and a possible
"err" property is not standard since variables that have simple values
generally do not also have any properties.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
>     if ( bolValue )  // true
>
> this follows "normal" coding practices.
>
> adding the '.err' to the boolean object, yes I admit, is
> a step beyond "standard", but I didn't think it was that
> far "from the tree".
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

it goes against everything that people learn about complex vs simple values
and what variables of each type can and cannot do.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> >  if(bolValue && !bolValue.err)
>
> I thought of that, used it even. It just didn't seem
> like the way to go with this.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

it certainly is more difficult to grasp how to use.  i'll give you that.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> PS: Jeff, there is a binary waiting for you.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

i know.  i've been hellishly busy and haven't had a chance to do anything
more than glance at the last email you sent me offlist.  maybe i can grab it
right now and give it a run tomorrow at work.

thanks,

.jeff

------------------------------------------------------
Jeff Howden - Web Application Specialist
Resume - http://jeffhowden.com/about/resume/
Code Library - http://evolt.jeffhowden.com/jeff/code/



More information about the thelist mailing list