- From: Dean Edwards <dean@edwards.name>
- Date: Thu, 11 May 2006 11:19:14 +0100
On 11/05/06, Lachlan Hunt <lachlan.hunt at lachy.id.au> wrote: > Dean Edwards wrote: > > It would be great if NodeLists were subclasses of JavaScript Array > > objects (especially with the introduction of Mozilla's Array Extras > > [1]). This makes iteration over DOM queries more flexible. > > It sounds like a nice idea, but there are some problems with it that I > can see. NodeLists are live, meaning that changes made to the > underlying DOM will be reflected in the list. Conversely, it means that > any changes made to the NodeList would logically need to be reflected in > the DOM. > > e.g. If you get a node list of P elements like this > > var pNodes = document.getElementsByTagName("p"); > > then later in the script you remove one of those P elements from the > DOM, it will also be removed from that NodeList. > > This is a problem for your idea because it means that changes made to > the NodeList would also need to be made to the DOM, in order to keep > them in sync. > > e.g. What would happen if you used the Array push() function on the > NodeList? > > pNodes.push(newNode); > > Where in the DOM would that new node be added? > I guess mutator methods should throw an error. Because we are subclassing Array, NodeLists don't have to behave /exactly/ the same as Array objects. I guess the really useful methods are the iterator methods introduced in recent versions of Firefox. Being able to use these on NodeLists would be great. -dean
Received on Thursday, 11 May 2006 03:19:14 UTC