Why does removeChild need a parent node?

删除回忆录丶 提交于 2019-12-29 05:57:06

问题


After answering this question I am left wondering why removeChild needs a parent element. After all, we could simply do

node.parentNode.removeChild(node);

As the parent node should be always directly available to the Javascript/DOM engine, it is not strictly necessary to supply the parent node of the node that is to be removed.

Of course I understand the principle that removeChild is a method of a DOM node, but why doesn't something like document.removeNode exist (that merely accepts an arbitrary node as parameter)?

EDIT: To be more clear, the question is: why does the JS engine need the parent node at all, if it already has the (unique) node that's to be removed?


回答1:


I think it keeps the design simple. A node may exist in isolation but the more interesting case is the DOM tree. With removeChild, the node to be removed must be a child of the node on which the method was called.

Getting a list of all children and doing a manual comparison against each is not that expensive an operation. However, searching all descendants for a node that is to be removed is indeed expensive.

Edit: In response to your update, a browser is simply implementing the DOM spec, which defines a removeChild method on Node. The spec, in my opinion, has to be unambiguous and free of assumptions. It is similar to Dependency Injection from that perspective. The DOM Core spec models a tree using building blocks such as Node, Element, etc. Adding a lone method such as removeNode somewhere in these building blocks means the method has implicit knowledge about its environment - that it may be a child of some node, and it should be removed from there if it is.

The task of w3 is to make a very robust API which makes most things possible. They shouldn't worry about syntactic sugar as that can always be written around the native APIs if they are well written.




回答2:


The confusion might be because you might think removing an element means something like killing or destroying it.

But in fact, the concept of removal basically means breaking the relationship between a child and its parent. It's just a detachment.

Therefore, removing a element which has no parent node makes no sense. And it's reasonable that, if you want to break that connection between a parent and a child, you need a reference to both.

That said, it's true that sometimes you just want to remove a child from its parent, without caring about that parent at all. That's why DOM Level 4 introduces the ChildNode interface, which provides the remove method.

That interface is implemented by DocumentType, Element and CharacterData, so you can use it on doctypes, elements and Text, Comment, and ProcessingInstruction nodes.

Assuming node is one of these, you can use

node.remove();

In case it already has no parent node, nothing happens.



来源:https://stackoverflow.com/questions/3422370/why-does-removechild-need-a-parent-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!