Javascript:为什么这个对象比较不起作用?

问题描述 投票:0回答:2

为什么第26行中的对象比较返回false,即使第25行清楚地显示元素是Text类型?使用===没有区别。

24        console.log(element)
25        console.log(element.constructor)
26        console.log(element.constructor == Text)

Console output

javascript
2个回答
1
投票

试试这个:

console.log(element.constructor.name === "Text")

1
投票

如果你想检查element的类型,你可以像这样使用instanceof

console.log((element instanceof Text)); // Logs true/false depending on the type of element

如果elementText对象,那么这将返回true

© www.soinside.com 2019 - 2024. All rights reserved.