Number.isNaN

John-David Dalton john.david.dalton at gmail.com
Fri Dec 14 10:48:17 PST 2012


Bendan Eich wrote:
> Your modernizr true-wrapping Boolean example is both a WTFJS moment,
easily avoided by using a truthy object as Sam pointed out, and nothing to
do with NaN.

The Modernizr example was in response to Axel's request for an example of
boxed values being used in real world projects.
I love how the thread got sidetracked by that one ;D

Popular libs like jQuery, Dojo, MooTools, Prototype, and Underscore have
`isXyz` methods or equivalents that equate boxed and unboxed values as
similar:
For example:
Underscore `_.isString('hi')` and `_.isString(Object('hi'))` both return
`true` also `_.isEqual('hi', Object('hi'))` returns `true`
MooTools `typeOf('hi')` and `typeOf(Object('hi'))` both return 'string'
Prototype `Object.isString('hi')` and `Object.isString(Object('hi'))` both
return `true`
jQuery `$.type('hi')` and `$.type(Object('hi'))` both return 'string'
Dojo `dojo.isString('hi')` and `dojo.isString(Object('hi'))` return `true`

`Object(NaN)` is edge case, but lots of things in the spec are edge (`-0`
anyone).
Because the majority of libs treat boxed and unboxed alike in their `isXyz`
I think the spec should follow.




On Fri, Dec 14, 2012 at 10:21 AM, Brendan Eich <brendan at mozilla.com> wrote:

> John-David Dalton wrote:
>
>> > But `myNaN === myNaN` is true if `myNaN = Object(NaN)`.
>>
>> That's my point. Normally testing for NaN can be done via `myNaN !==
>> myNaN` but `Object(NaN)` throws a wrench in that.
>> It would be great if there was 1 function that was able to detect NaN,
>> instead of having libs step up and do it.
>>
>
> Why? Who wraps NaN? Your modernizr true-wrapping Boolean example is both a
> WTFJS moment, easily avoided by using a truthy object as Sam pointed out,
> and nothing to do with NaN.
>
> /be
>
>>
>> -JDD
>>
>>
>>
>> On Fri, Dec 14, 2012 at 9:12 AM, Nathan Wall <nathan.wall at live.com<mailto:
>> nathan.wall at live.com>> wrote:
>>
>>     > Wat? This seems to be a good reason to allow `Object(NaN)` and
>>     use the
>>     > NumberWrapper brand as it cannot be tested via the normal way of
>>     > `myNaN !== myNaN`.
>>
>>     But `myNaN === myNaN` is true if `myNaN = Object(NaN)`. Testing
>>     against the object is different. Nothing breaks.
>>
>>         var myNaN = Object(NaN);
>>         [ 1, 3, myNaN ].indexOf(myNaN); // => 2
>>
>>     Works as expected. The only problem which occurs is when you're
>>     working with primitive NaN, in which case the only existing good
>>     ways to test for it are `x !== x` and `typeof x == 'number' &&
>>     isNaN(x)`. The purpose of `Number.isNaN` is to provide a way to
>>     test this case which is easier to read and understand. Note that
>>     if `x = Object(NaN)` both of these tests fail.
>>
>>     Nathan
>>
>> ______________________________**_________________
>> 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/20121214/9791f57f/attachment.html>


More information about the es-discuss mailing list