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

Re: Detecting the "type" of a DOM object

From: Boris Zbarsky <bzbarsky@MIT.EDU>
Date: Wed, 06 Jun 2012 17:35:56 -0400
Message-ID: <4FCFCD3C.6000200@mit.edu>
To: David Bruant <bruant.d@gmail.com>
CC: "public-script-coord@w3.org" <public-script-coord@w3.org>
On 6/6/12 5:22 PM, David Bruant wrote:
> The equivalent is possible now for DOM elements
> (Object.prototype.toString.call(document) returns "[object
> HTMLDocument]"), although to know whether something is a Node, you need
> to know all possible subclasses.

Exactly the problem.

Note that in IE9 "Object.prototype.toString.call(document)" returns 
"[object Document]", by the way, so it's not all fun-and-games even for 
basic usage.

>> 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.
> While it wouldn't be perfectly future-proof (since new subclass of Nodes
> can come along later), the technique I've explained above does work
> across globals.

For the specific cast of Node... last I checked, there are 100+ 
subclasses of Node in the platform, with new ones being added about 
monthly.  The technique you described simply doesn't work for nodes....

> I don't like isInstanceOf,

I don't either.  We need a better name.  ;)

-Boris
Received on Wednesday, 6 June 2012 21:36:26 UTC

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