Boolean objects in conditionals

Lasse R.H. Nielsen atwork at infimum.dk
Fri Nov 28 14:20:49 PST 2008


On Fri, 28 Nov 2008 22:42:45 +0100, Peter Michaux <petermichaux at gmail.com> wrote:

> The following suggests (i.e. could be interpreted as) a false Boolean
> object is a "truthy" value.
>
>> (new Boolean(false)) ? "a" : "b"
> "a"

Yes. 

> The following suggests a false Boolean object is a "falsy" value
>
>> ((new Boolean(false)) == true) ? "a" : "b"
> "b"

It clearly shows that "new Boolean(false)" is not equal to "true". 
However, there are lots of non-falsy values that are not equal to true, so I don't
see it suggesting anything about the falsy-ness of "new Boolean(false)", any more
than  
  ((new String("pickles") == true) ? "a" : "b"


> I understand what is going on here but it seems like a bad state of affairs.

Wrapper objects are generally bad, if you ask me. 
They serve no purpose, except as a specification device for allowing properties
on basic values.

> Does this need to be remedied? Can it be?

I don't think it is needed. The current rules are sometimes surprising, but
they are clear: all objects are considered true.

> It would be useful to have an object that is effectively "false" in an
> "if" conditional but which can also have properties like an object
> can.

If objects are no longer all "true", then I would prefer each object to
be able to decide its truth value. 
However, it would probably break a lot of idiomatic constructs, like
 if (obj) { obj.method(); }
or
 var x = obj || defaultObj;

/L
-- 
Lasse R.H. Nielsen
'Faith without judgement merely degrades the spirit divine'


More information about the Es-discuss mailing list