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

Check if the variable is a SVG.js element instance

本文关键字:js 元素 实例 SVG 变量 是否 检查      更新时间:2023-09-26

我正在使用svg.js库。

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


我试过了:

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

检查函数返回的值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