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 ] );
  else
    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
http://hiveminds.info/
http://soopahman.com/
Received on Wednesday, 3 December 2003 13:40:45 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:57 GMT