Check if the variable is a SVG.js element instance

613 Views Asked by At

I'm using the svg.js library.

How can we check if a variable x is an instance of SVG class?


I tried:

new SVG(document.createDocumentFragment()) instanceof SVG     // false
new SVG(document.createDocumentFragment()).contructor === SVG // false
1

There are 1 best solutions below

1
On BEST ANSWER

Checking what value SVG function returns we find that a new element is created and returned. It is an instance of SVG.Doc:

> SVG
svg.js:12 function (element) {
    if (SVG.supported) {
      element = new SVG.Doc(element)

      if (!SVG.parser)
        SVG.prepare(element)

      return element
    }
  }

So, the solution is:

new SVG(document.createDocumentFragment()) instanceof SVG.Doc // true

// or using the x variable
var x = new SVG(document.createDocumentFragment());
x instanceof SVG.Doc // true