W3C home > Mailing lists > Public > public-script-coord@w3.org > April to June 2012

Detecting the "type" of a DOM object

From: Boris Zbarsky <bzbarsky@MIT.EDU>
Date: Wed, 06 Jun 2012 14:59:56 -0400
Message-ID: <4FCFA8AC.3050002@mit.edu>
To: "public-script-coord@w3.org" <public-script-coord@w3.org>
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

This archive was generated by hypermail 2.3.1 : Wednesday, 8 May 2013 19:30:06 UTC