Re: Detecting the "type" of a DOM object

Le 21/06/2012 07:30, Boris Zbarsky a écrit :
> On 6/20/12 11:20 PM, Cameron McCormack wrote:
>> I think what I really want to write is:
>>
>> if (n.is(Node))
>> ...
>>
>> but I don't think that will fly.
>
> What if we used a catapult?  It looks _really_ nice.  ;)
>
> The real problem with it is that it will throw for random non-DOM 
> objects, which we really can't require to have is() on them....
The existential operator [1], if part of ES6, could enable robust test 
at small cost if really necessary:

     if(n?.is(Node))

But if the test is being used in circumstances where it's guaranteed 
that what is passed is DOM objects, then the ? could be removed.

> How much would people hate a "n is Node" language feature akin to 
> instanceof but more flexible?
I'm not a big fan of the idea of introducing new syntax specifically to 
fix the DOM and with no other purpose.
Also, an "is" operator is already on the way for another purpose [2]

> Alternately, we could try to make instanceof actually work cross-global?
+1, but eternal web worry: won't it break the web? But that would be my 
favorite solution overall.
Would it apply to non-DOM objects?
Specifically, I think that some folks use cross-global arrays.
I assume that by making instanceof work globally you mean breaking its 
definition based on prototype object identity?

David

[1] http://wiki.ecmascript.org/doku.php?id=strawman:existential_operator
[2] 
http://wiki.ecmascript.org/doku.php?id=harmony:egal#is_and_isnt_operators

Received on Thursday, 21 June 2012 08:16:30 UTC