简体   繁体   English

Typescript ChildNode:`parentElement` 和/或 `parentNode` 是 null?

[英]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.parentElementChildNode.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 of Node objects that can have a parent. ChildNode mixin 包含所有类型的可以有父节点的Node对象通用的方法和属性。 It's implemented by Element , DocumentType , and CharacterData objects.它由ElementDocumentTypeCharacterData对象实现。

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.

相关问题 typescript parentElement 返回值 - typescript parentElement return value 如何保护ChildNode? 还是使用带有Typescript的ChildNode方法? - How to typeguard ChildNode? Or use ChildNode methods with Typescript? $(...).parentElement 不是 function - WebDriver IO / TypeScript 问题 - $(...).parentElement is not a function - WebDriver IO / TypeScript issue ChildNode [Typescript] 类型上不存在属性“tagName” - Property 'tagName' does not exist on type ChildNode [Typescript] 单元测试期间无法读取 null 的属性(读取“parentElement”) - Cannot read properties of null (reading 'parentElement') during Unit Test 如何让 React.js TypeScript 通过 onKeyDown 事件接受 'event.target' 上的 'parentElement'? - How can I get React.js TypeScript to accept 'parentElement' on 'event.target' with a onKeyDown event? '(Node & ParentNode) | 类型的参数 null' 不可分配给“元素”类型的参数。 类型“null”不可分配给类型“元素” - Argument of type '(Node & ParentNode) | null' is not assignable to parameter of type 'Element'. Type 'null' is not assignable to type 'Element' React TypeError:在 document.getElementByID JEST TESTING 之后无法读取 null 的属性“parentNode” - React TypeError: Cannot read property 'parentNode' of null after document.getElementByID JEST TESTING Jasmine 如何模拟parentElement - Jasmine how mock parentElement 对象在打字稿中可能是“空” - object is possibly 'null' in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM