2

Possible Duplicate:
Why does removeChild need a parent node?

Cross-browsers way to remove a node element is to use removeChild() method. However, this way expects us to precise the node's parent as follows:

myNodeToRemove.parentNode.removeChild(myNodeToRemove);

Why didn't browsers implement the remove method with a more object-oriented way like this:

myNodeToRemove.remove();

With remove() method starting as follows:

function remove(){
  var parentNode = this.parentNode;
  ....
}

Indeed, using this way, no need to manually get the node's parent.

Community
  • 1
  • 1
Mik378
  • 21,881
  • 15
  • 82
  • 180
  • 1
    Probably to replicate that of `appendChild()`, `replaceChild()` and `insertBefore()`. Who knows, though? It's what the nice guys at W3 decided. – jeremy Nov 10 '12 at 19:08
  • Maybe... I find that very weird. – Mik378 Nov 10 '12 at 19:14
  • 2
    I guess its the parent's responsibility to decide if its child has become useless enough to end the suffering.. its all about ethics. On a less serious note look here http://stackoverflow.com/a/3422497/149636 – lostsource Nov 10 '12 at 19:14
  • @dystroy I already read this post, but it doesn't really answer the same question. – Mik378 Nov 10 '12 at 19:37
  • I've read again the question, it's exactly the same. – Denys Séguret Nov 10 '12 at 21:42
  • 1
    This is a duplicate of the earlier question, *and* "not constructive" (see the FAQ, this requires opinion and speculation). – T.J. Crowder Nov 11 '12 at 00:01

2 Answers2

1

For the question:

Why didn't browsers implement the remove method with a more object-oriented way like this

myNodeToRemove.remove();

...you need to study Javascript history, how the language was born, how APIs were born and so on. Then you understand how we ended up with the clusterfuck called modern web. The history is hilarious.

Good place to start is to watch Crockford on Javascript videos

http://javascript.crockford.com/#video

Also regarding function naming and object-oriented practices this is a good video:

http://vimeo.com/43380467

Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
0

I think the reason to use the combination of parentNode and removeChild ensures that we are referencing the removeChild method on the actual parent of the container that we wish to remove.

defau1t
  • 10,593
  • 2
  • 35
  • 47