为什么第26行中的对象比较返回false,即使第25行清楚地显示元素是Text类型?使用===没有区别。
24 console.log(element)
25 console.log(element.constructor)
26 console.log(element.constructor == Text)
试试这个:
console.log(element.constructor.name === "Text")
如果你想检查element
的类型,你可以像这样使用instanceof
:
console.log((element instanceof Text)); // Logs true/false depending on the type of element
如果element
是Text
对象,那么这将返回true