为什么 `Element instanceof Node` return false

why `Element instanceof Node` return false

我不知道为什么 Element instanceof Node return 错误,因为任何节点类型为 1 的元素都是一种特殊类型的节点。

以下是我从 MDN 上找到的:

The following interfaces all inherit from Node its methods and properties: Document, Element, CharacterData (which Text, Comment, and CDATASection inherit), ProcessingInstruction, DocumentFragment, DocumentType, Notation, Entity, EntityReference

以及问题2:为什么DOM中的任何元素既是instanceof元素又是instanceof Node.The代码如下:

var div = document.querySelector("div");
div instanceof Node;//true
div instanceof Element;//true
Element instancof Node;//false

Element 是一个 Function 因为它是一个构造函数。

Element.prototype 是一个 Node.

since any element , whose nodeType is 1, is a special type of Node.

请注意 Element.nodeTypeundefined,因为 Element 不是 Node

Why are elements in the DOM both instances of Element and Node?

原型链允许我们本质上定义 classes 并扩展定义的 classes.

function Foo() {...}

创建可以被认为是 Foo

的 class
function Bar() {...}

创建可以被认为是 Bar

的 class
Foo.prototype = new Bar(...);

创建关系,其中 Foo 扩展 Bar

使用此代码,如果您创建一个 Foo 实例:

var f = new Foo();

fFoo 的实例,Bar 的实例。

相同的层次结构适用于 ElementNode,其中 Element 扩展 Node