Re: Detecting the "type" of a DOM object

Le 21/06/2012 16:30, Boris Zbarsky a écrit :
> On 6/21/12 4:15 AM, David Bruant wrote:
>> Le 21/06/2012 07:30, Boris Zbarsky a écrit :
>>> Alternately, we could try to make instanceof actually work 
>>> cross-global?
>> +1, but eternal web worry: won't it break the web?
>
> It works cross-global for DOM objects in Gecko right now.
Really? After check, yes indeed. That's excellent. I thought it wasn't 
the case. I think what you said in your original message is what misled 
me into thinking that:
"There's a common problem where someone has an object and they would 
like to tell whether it's a Node (or an Array or a Date or whatever).  
Right now, the way to do that is to use instanceof.... but with a huge 
caveat: you have to pull the Node or Array or whatnot off the right 
global object."
Apparently, this sentence was inaccurate for Node objects.

So in Firefox, instanceof can be used with browser API (DOM + 
localStorage + XHR + whatev's is built-in to the browser) objects to 
know what "type" is an object and for Array and Date and other 
ECMAScript objects, checking the [[Class]] property can reliably be used 
across globals.
So it seems that the way Firefox model objects it's possible to detect 
the type of any object, WebIDL or ECMAScript-based across globals, no?

> We're actually deliberately breaking that in the WebIDL bindings at 
> the moment, so the actual web compat breakage for us is in the other 
> direction: as we move to WebIDL with its stricter instanceof sites 
> might break.
Of course. Then +1000 for changing how instanceof is defined in WebIDL 
to adopt Gecko behavior!


>> I assume that by making instanceof work globally you mean breaking its
>> definition based on prototype object identity?
>
> Yes.  In Spidermonkey there is a hook that can be attached to a given 
> [[Class]] that will get called if the object is the RHS of instanceof 
> and can do whatever it wants.  In the case of current DOM objects it 
> just checks whether the object implements the given interface, for 
> example.
Interesting. I don't know whether this can be implemented with proxies 
(for which one use case is to be able to implement the browser APIs in 
pure JS). I'll think about it.

David

Received on Thursday, 21 June 2012 14:50:30 UTC