node.insertBefore( child )

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 UTC