[英]Typescript ChildNode: `parentElement` and/or `parentNode` is null?
So here is a philosophical question.所以这是一个哲学问题。
In TypeScript a ChildNode can be parentless, since both ChildNode.parentElement
and ChildNode.parentNode
have a null
type option.在 TypeScript 中, ChildNode 可以是无父节点,因为ChildNode.parentElement
和ChildNode.parentNode
都有null
类型选项。
But being Child Node wouldn't it, by the nature of it, have a parent?但是作为子节点,从本质上讲,它不会有父节点吗? I can understand that parentElement
might be null
, since it might derrive from a non element node (or would that even be possible) but shouldn't at least parentNode
be non-null?我可以理解parentElement
可能是null
,因为它可能来自非元素节点(或者甚至可能),但至少parentNode
不应该是非空的?
I understand that it is not really specifically indicated in the DOM spec , but wouldn't it be strange to have a ChildNode
with no parent?我知道它并没有在DOM 规范中特别指出,但是有一个没有父节点的ChildNode
会不会很奇怪?
Is it like this in the case that you might move the node away from the parent?在您可能将节点从父节点移开的情况下是这样吗? But wouldn't something like this change the type of the ChildNode
, to plain ol' Node
?但是这样的事情不会将ChildNode
的类型更改为普通的Node
吗?
I would be really interested in having your insight on this question, as it has left me a bit puzzled.我真的很想知道你对这个问题的见解,因为它让我有点困惑。
From https://developer.mozilla.org/en-US/docs/Web/API/ChildNode :从https://developer.mozilla.org/en-US/docs/Web/API/ChildNode :
The
ChildNode
mixin contains methods and properties that are common to all types ofNode
objects that can have a parent.ChildNode
mixin 包含所有类型的可以有父节点的Node
对象通用的方法和属性。 It's implemented byElement
,DocumentType
, andCharacterData
objects.它由Element
、DocumentType
和CharacterData
对象实现。
ChildNode does not mean "this node currently has a parent node". ChildNode 并不意味着“这个节点当前有一个父节点”。 It's just a mixin that other types implement to include the remove()
, before()
, after()
, and replaceWith()
methods.它只是其他类型实现的 mixin,包括remove()
、 before()
、 after()
和replaceWith()
方法。
An example of a type that inherits from Node but does not mix in the ChildNode methods is Document .从 Node 继承但不混入 ChildNode 方法的类型的一个示例是Document 。 A Document is a Node (its children's parentNode
would be itself) but it can never have a parent node, so document.remove()
produces a compilation error. Document 是一个节点(其子节点的parentNode
将是它自己),但它永远不会有父节点,因此document.remove()
会产生编译错误。
Is it like this in the case that you might move the node away from the parent?在您可能将节点从父节点移开的情况下是这样吗? But wouldn't something like this change the type of the
ChildNode
, to plain ol'Node
?但是这样的事情不会将ChildNode
的类型更改为普通的Node
吗?
This isn't how TypeScript's static type checking works — calling a method such as remove()
on an object doesn't change its type.这不是 TypeScript 的 static 类型检查的工作原理——在 object 上调用诸如remove()
之类的方法不会改变其类型。 (It might be technically possible in JavaScript to change an object's prototype, but TypeScript doesn't model this.) The node is still an object of the same class it was before removal, so it still implements these methods, even though calling them when the node has no parent might not make sense. (It might be technically possible in JavaScript to change an object's prototype, but TypeScript doesn't model this.) The node is still an object of the same class it was before removal, so it still implements these methods, even though calling them when节点没有父节点可能没有意义。 And in fact, since every Element is a ChildNode , dynamically changing the type after element.remove()
wouldn't make sense because the element should still be an Element.事实上,由于每个 Element 都是一个 ChildNode ,因此在element.remove()
之后动态更改类型是没有意义的,因为该元素仍然应该是一个 Element。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.