Logical operators don't use valueOf()

Marius Gundersen gundersen at gmail.com
Sun Sep 8 15:20:07 PDT 2013


The only thing that is unclear is the reason memoizatation isn't used. It
would seem to be the way most of the js engines would implement this
anyways. I'm not familiar with the inner workings of JavaScript JIT
engines, but in a situation like this, would that code not be compiled into
something which stores the truthyness of obj in a register, until the
expression is completely evaluated?

Thanks for the detailed information you can provide on this subject btw.

Marius Gundersen
Den 8. sep. 2013 21:43 skrev "Brendan Eich" <brendan at mozilla.com> følgende:

> Did you read my reply on this point?
>
> https://mail.mozilla.org/**pipermail/es-discuss/2013-**
> September/033234.html<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<https://mail.mozilla.org/listinfo/es-discuss>
>> Till Schneidereit <mailto:till at tillschneidereit.**net<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/~jorendorff/es6-draft.html#sec-7.1.2>
>>     <http://people.mozilla.org/%**7Ejorendorff/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<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/~jorendorff/es6-draft.html#sec-7.1.2><
>> http://people.mozilla.org/%**7Ejorendorff/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<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/~jorendorff/es6-draft.html#sec-7.1.2><
>> http://people.mozilla.org/%**7Ejorendorff/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<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<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<https://mail.mozilla.org/listinfo/es-discuss>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130909/c3e981e4/attachment.html>


More information about the es-discuss mailing list