user730569
user730569

Reputation: 4004

How do check whether a DOM node inherits from a constructor?

I have a random DOM node and I want to determine whether it is an svg element, namely that is inherits from the SVGElement constructor. I know I could just walk up the prototype chain by calling __proto__ on the node, but is there a built in method to determine this?

Upvotes: 4

Views: 267

Answers (1)

pimvdb
pimvdb

Reputation: 154868

There is the dedicated instanceof operator which checks whether an object has a constructor's prototype in its prototype chain:

node instanceof SVGElement

However, given that you cannot actually do new SVGElement() (as with all node constructors), this may not work reliably across all browsers.

Upvotes: 1

Related Questions