I'm traversing the DOM and one of the divs I analyze looks different than the rest. All other divs:
document.querySelector('#somediv') instanceof Node
// true
But one div:
document.querySelector('#strange_div') instanceof Node
// false
When I check the constructor of that div:
document.querySelector('#strange_div').constructor.name
// HTMLDivElement
And yet, when I test instanceof again:
document.querySelector('#strange_div') instanceof HTMLDivElement
// false
How is it possible ? A div that isn't an instance of Node, yet is an instance of HTMLDivElement
Note The div is of type ELEMENT_NODE
document.querySelector('#strange_div').nodeType
// 1
Checking the __proto__
chain
(7) [div#strange, HTMLDivElement, HTMLElement, Element, Node, EventTarget, {…}]
0: div#strange
1: HTMLDivElement {Symbol(Symbol.toStringTag): "HTMLDivElement", constructor: ƒ}
2: HTMLElement {…}
3: Element {…}
4: Node {…}
5: EventTarget {Symbol(Symbol.toStringTag): "EventTarget", addEventListener: ƒ, dispatchEvent: ƒ, removeEventListener: ƒ, constructor: ƒ}
6: {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
Comparing the Node in the proto chain:
document.querySelector('#strange').__proto__.__proto__.__proto__.__proto__ === Node
// false
Note 2
My intention here is to override the Node element with a specific getter function. This getter is available to all the divs but the one which isn't an instance of Node.
In more details:
childNodes
is extended by some 3rd party library and that's why it's unusable to me.
I created a new iframe and I extend the Node element with a pure reference to the childNodes
getter
const ref =
Object.getOwnPropertyDescriptor(
safeReferenceFromIframe.Node.prototype,
functionName
) || {};
Object.defineProperty(Node.prototype, `pure_${functionName}`, ref);