Logical operators don't use valueOf()

Brendan Eich brendan at mozilla.com
Sun Sep 8 12:43:48 PDT 2013


Did you read my reply on this point?

https://mail.mozilla.org/pipermail/es-discuss/2013-September/033234.html

Anything unclear?

/be

> Marius Gundersen <mailto:gundersen at gmail.com>
> September 8, 2013 3:47 AM
> So how about implementing toBoolean as an overridable method, which is 
> called whenever !, || or && are used. This way an objec has three 
> method for converting it into a primitive; toString, toValue and 
> toBoolean.
>
> Marius Gundersen
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> Till Schneidereit <mailto:till at tillschneidereit.net>
> September 7, 2013 5:19 PM
> On Sun, Sep 8, 2013 at 2:15 AM, Marius Gundersen <gundersen at gmail.com 
> <mailto:gundersen at gmail.com>> wrote:
>
>     > That's the way ToBoolean has always been defined
>     http://people.mozilla.org/~jorendorff/es6-draft.html#sec-7.1.2
>     <http://people.mozilla.org/%7Ejorendorff/es6-draft.html#sec-7.1.2> .
>     All objects are considered to be Boolean true values.
>
>     So does this mean I can override toBoolean to change the way !
>     behaves on an object?
>
> No, ToBoolean is a spec-internal abstract operation. I.e., it's a 
> collection of steps to be taken whenever the operation is required by 
> the specification of some other operation, but it's not available to 
> JS code.
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> Marius Gundersen <mailto:gundersen at gmail.com>
> September 7, 2013 5:15 PM
>
> > That's the way ToBoolean has always been defined 
> http://people.mozilla.org/~jorendorff/es6-draft.html#sec-7.1.2 
> <http://people.mozilla.org/%7Ejorendorff/es6-draft.html#sec-7.1.2> . 
> All objects are considered to be Boolean true values.
>
> So does this mean I can override toBoolean to change the way ! behaves 
> on an object?
>
> Marius Gundersen
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> Allen Wirfs-Brock <mailto:allen at wirfs-brock.com>
> September 7, 2013 11:08 AM
>
>
>
> That's the way ToBoolean has always been defined 
> http://people.mozilla.org/~jorendorff/es6-draft.html#sec-7.1.2 
> <http://people.mozilla.org/%7Ejorendorff/es6-draft.html#sec-7.1.2> . 
> All objects are considered to be Boolean true values.
>
> &, | ,^, ~ are actually bitwise operations on numeric values so they 
> do ToInt32 (which may ultimately call valueOf).
>
> !, &&, || are logical boolean operations so they call ToBoolean.
>
> This generally makes sense, and far too much existing code depends 
> upon this behavior to every consider changing it.
>
> Allen
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> Marius Gundersen <mailto:gundersen at gmail.com>
> September 7, 2013 9:10 AM
> While playing around with valueOf, I discovered that three operators 
> (!, ||, &&) don' seem to use valueOf when you use them. You can test 
> this in your preferred browser here: 
> http://lab.mariusgundersen.net/valueOf
>
> Is there a reason for this? Shouldn't these operators work the same 
> way as the other operators?
>
> Marius Gundersen
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list