Re: An observation about "live" NodeLists

Ray Whitmer <ray@imall.com> writes:

> Stephen R. Savitzky wrote:
> 
> > I think that the naive script writer is more likely to write something like
> > my snippet (which blows up) or yours (which is written much more carefully
> > and so only has _one_ bug in it) and spend a lot of time head-scratching.
> 
> Liveness is the rule in object models.  It is grade-school programming to
> understand that if you remove an object, the indices shift.

The non-obvious thing about live NodeLists is that the indices shift, NOT
when you remove the objects from NodeList (which you can't do, anyway), but
when you remove them from the _Document_.  

The problem is that the NodeList _looks_ like an ordinary container, like an
array.  The programmer expects that, whatever happens to the objects
someplace _else_ in the system, that they will stay in this apparently-new
container that was just constructed to hold them.

> Perhaps you could cite for me examples in the familiar Java API where a static
> snapshot of a live object model's contained/collected list is returned for
> indexing or iterating instead of the live model. 

Vector, for example.  An object can be in several different Vectors, and
removing it from one does not remove it from another.  Suppose you build a
tree in which nodes have a Vector of children.  You expect that if you ask a
Node for its "children" that you will get a "live" reference to its actual
children Vector, but if you have a recursive search operation like
findElementsByTagName, you expect that it will return a _new_ Vector
containing references to the elements you just found.

-- 
 Stephen R. Savitzky   Chief Software Scientist, Ricoh Silicon Valley, Inc., 
<steve@rsv.ricoh.com>                            California Research Center
 voice: 650.496.5710   fax: 650.854.8740    URL: http://rsv.ricoh.com/~steve/
  home: <steve@starport.com> URL: http://www.starport.com/people/steve/

Received on Friday, 16 October 1998 19:41:36 UTC