W3C home > Mailing lists > Public > www-dom@w3.org > October to December 2003

node.insertBefore( child )

From: Chris Moschini <cmoschini@myrealbox.com>
Date: Wed, 03 Dec 2003 13:34:37 -0500
To: www-dom@w3.org
Message-ID: <1070476477.7b6a6c80cmoschini@myrealbox.com>

As long as we're on the subject of .insertBefore()

Why does this method, when no second argument is specified, append the child? .appendChild already does this - why not make .insertBefore() insert at the beginning by default?

Opportunity for added functionality aside, consider that a loop to add elements to the end of a list is simple:

for( i = 0; i < len; i++ )
  node.appendChild( list[ i ] );

Whether the node begins empty or not. But a loop to add elements to the beginning is not:

for( i = 0; i < len; i++ ) {
  if( node.childNodes.length == 0 )
    last = node.appendChild( list[ i ] );
    node.insertBefore( list[ i ], last );

While I realize the for loop could be optimized, that's not the point. There are many varied cases where objects must be repeatedly added to any number of lists, which may or may not be empty at the time. The call may not happen directly inside of a for loop, but several methods down, the top-most being called in such a loop, etc.

Why not make this call as simple as:
node.insertBefore( list[ i ] );


-Chris "SoopahMan" Moschini
Received on Wednesday, 3 December 2003 13:40:45 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 20 October 2015 10:46:11 UTC