Re: DOM collections index out of bounds and JavaScript.

On 10/19/10, Garrett Smith <dhtmlkitchen@gmail.com> wrote:
> On 10/19/10, Scott Shattuck <idearat@mindspring.com> wrote:

[...]
> I don't generally write code that accesses an indexed property of a
> collection unless I know that the element at that index exists. I
> recall now a piece of code -- jQuery, in fact-- wherein an "optimized"
> `for` loop was implicated in an out-of-bounds property access on a
> StyleSheetList object. The result was a javascript error in MSIE. The
> author blamed MSIE for that, but when I saw that code, I realized that
> with as much js as I'd written, and even in using MSIE's collection
> equivalent of styleSheetList, that I'd never tried to access an
> out-of-bounds index off it. I'd always done like so:
>
> for(var i = 0; i < list.length; i++) {
>   if( list[i].title == "foo") {
>     return list[i];
>   }
> }
>
> And so with that code, there would never be an out-of-bounds access.
>
> I forget the thread title but I'll try and dig it up...
>
> FOund:
> <http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/41e87ec5cbcbdaff/785e03f0701777c0?>
>

I was later reminded of another problem with accessing a not-defined
property off a NodeList from a detached node in older versions of
Webkit.

http://github.com/jquery/jquery/commit/94f35d05199ec1634d9c8c60d10f298f260056bd#commitcomment-135849

"In Safari 2, given an Element not in the document, getting a nodeList
off that element (e.g. myDiv.childNodes) and then attempting to access
a non-existing property such as myDiv.childNodes.setInterval, will
crash the browser. I can't remember if the element had to be removed
from the document or not. NEver bothered me that much, as I tend not
to do such things on my own, so the details are not that interesting
(and so forgettable)."

Read down that thread to see what actions the author took in response
to that explanation. The whole thread is a really good read, actually.

Garrett

Received on Tuesday, 26 October 2010 21:48:39 UTC