Re: Detecting the "type" of a DOM object

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