Detecting the "type" of a DOM object

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.

I was wondering whether it would make sense to expose, on DOM 
constructor objects, a method that lets you test whether some other 
object is of that type.  Note that this functionality is already present 
to some extent because the actual methods on the proto have to test that 
the |this| is of the right type.

So you would be to do something like Node.isInstanceOf(myobj) (or 
something along those lines) and this would return true if myobj is a 
Node, even if it's a Node from a different global.

Thoughts?  This is somewhat similar to the Array.isArray thing ES 
already has...

There are some complications with WebIDL "implements" statements that 
I'm not sure how to deal with, but I think it's pretty rare for people 
to worry about whether something is an instance of the RHS of an 
"implements".

-Boris

Received on Wednesday, 6 June 2012 19:00:31 UTC