简体   繁体   English

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

[英]Check if the variable is a SVG.js element instance

I'm using the svg.js library. 我正在使用svg.js库。

How can we check if a variable x is an instance of SVG class? 我们如何检查变量x是否是SVG类的实例?


I tried: 我试过了:

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

Checking what value SVG function returns we find that a new element is created and returned. 检查SVG函数返回的值后,我们发现已创建并返回了一个新element It is an instance of SVG.Doc : 它是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM