0

Why does the object comparison in line 26 return false, even though, line 25 clearly shows element is of type Text? Using === makes no difference.

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

Console output

  • There is no information to go on here. Can you show actual code? You likely need to read this: https://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator – mplungjan Dec 21 '17 at 11:41
  • 1
    Works fine for me, with a regular `Text` node. What is `element` precisely? Where exactly do you execute that code — on an environment that has a globally accessible `Text` constructor? Please, [edit] your question and provide a [mcve]. – Sebastian Simon Dec 21 '17 at 11:43

2 Answers2

1

Try using this:

console.log(element.constructor.name === "Text")
Jon
  • 10,678
  • 2
  • 36
  • 48
1

If you are trying to check the type of the element you can use the instanceof like so:

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

If element is a Text object then this will return true

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64