Wednesday, 23 January 2019

Can I use instance-of features to distinguish one subclass of Node from another

Can I use instance-of features to distinguish one subclass of Node from another?

Answer:

That may work in some implementations of the DOM, but it isn't portable and should be avoided. Not all languages support this kind of runtime type identification. Even in languages which do, the results will depend on exactly how the DOM was implemented.
The specification does not guarantee that there is a one-to-one mapping from DOM interface to actual object type. It is entirely possible that a single class might have been written to implement more than one of the DOM interfaces. In such an implementation, the language features can't tell which of those interfaces is actually in use by a given instance of the class. For example, if the DOM developer decided to implement Text and Comment nodes using a single class, an instance of this class would be recognized as being a legitimate instance of both interfaces.

To reliably tell which kind of Node you're looking at, you should look at its nodeType value. To distinguish HTMLElements, look at their nodeName.

No comments:

Post a Comment