typeof null

Brendan Eich brendan at mozilla.org
Wed May 9 14:43:56 PDT 2012


John J Barton wrote:
> On Wed, May 9, 2012 at 2:22 PM, Brendan Eich<brendan at mozilla.org>  wrote:
>> As Crock said, the problem programmers face is "I have a value, I want to
>> treat it as an object and get a property (call a method, etc.). How do I
>> test is-this-an-object so that I can use dot without worrying about that
>> throwing right away on null left-hand side?"
>
> Yes, but ... why not
>     if (foo) {
>        foo.bar()
>     }
> or one of its variants? I suppose that
>    if (typeof foo === 'object') { ...
> helps in the case the your caller sent a string etc, but if this is
> your goal, then why not use
>    if (foo&&  foo.bar) {
> and avoid other left-hand side problems?

I like falsy tests but in really generic code, as you suggest, primitive 
falsy values could creep in and should work.

Testing for the wanted property being truthy may not be enough (if it 
must be callable) but at much production code does not have to guard 
against null because of guaranteed initialization or small-world 
codebase rules that fit in maintainers' heads.


> (FWIW I don't really think this is a big deal one way or another).

Agreed. Adding Object.isObject this late won't help for downrev code and 
the polyfill, even though trivial, is probably not worth the trouble. My 
gut says so, anyway -- different developers may disagree.

I think we should consider Object.isObject just because the typeof null 
change replaced it, but 1JS killed that replacement. Also gives 
Object.isValue and Array.isArray some company ;-).

/be
>
> jjb
>
>> /be
>>
>> John J Barton wrote:
>>> On Tue, May 8, 2012 at 6:07 PM, Brendan Eich<brendan at mozilla.org>    wrote:
>>>> Object class reflection is frowned upon in Smalltalk for a reason. We
>>>> want
>>>> protocols, structural conventions -- not nominal type tags. Or so I
>>>> think!
>>> Perhaps it would be helpful if someone made the case for typeof null
>>> === 'null'.
>>>
>>> To me typeof null === 'object' is fine. It makes null a value in the
>>> space of 'object'. In practice I see 'null' used to mean "I know this
>>> reference (usually an argument) should be an object; I want to pass
>>> nothing but signal that I really did mean to pass nothing." The status
>>> quo allows this and it seems enough work for null to do for us.
>>>
>>> Are there new things I can do if I now have a new answer to the
>>> 'typeof' question?
>>>
>>> jjb
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss at mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list