检查变量是否是SVG.js元素实例

时间:2014-12-16 10:46:06

标签: javascript svg svg.js

我正在使用svg.js库。

我们如何检查变量x是否是SVG类的实例?


我试过了:

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

1 个答案:

答案 0 :(得分:0)

检查SVG函数返回的值,我们发现创建并返回了新的element。它是SVG.Doc

的一个实例
> SVG
svg.js:12 function (element) {
    if (SVG.supported) {
      element = new SVG.Doc(element)

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

      return element
    }
  }

所以,解决方案是:

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

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