First of all, for..in is not the same as forEach.
In second place, iterating arrays with `for..in` is just stupid, it is meant
to be used with objects in general. It's well known that `for..in` is
extremely inefficient when compared to length-cached `for` loops.
So, you should be doing

var foo = [1,2,3];
for(var i = 0, len = foo.length; i < len; ++i)
  //do stuff with foo[i]

If you take a look at what the `for..in` statement does[1], you should see
that it iterates over all the object's properties, but not only his own,
also the ones that he inherits from his constructor's prototype property.
That's why you see all the Array[2] methods when you iterate a
prototype-extended array with `for..in`.

[1]
https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Statements/For...in
[2] http://prototypejs.org/api/array

Gabriel Gilini

www.usosim.com.br
gabr...@usosim.com.br


On Fri, May 1, 2009 at 3:35 PM, GlennFleishman <gl...@glennf.com> wrote:

>
> I checked back discussions and I don't see this addressed. There's a
> collision between Prototype and certain kinds of array calls as
> documented at
> http://zurahn.wordpress.com/2009/01/11/prototypejs-breaks-javascript-foreach-loops/
>
> I saw this behavior on my site when I added an Amazon product pop-up
> JavaScript that apparently makes a "for (var foo in array)" call. I've
> notified Amazon as to the problem, in case they can fix it easily on
> their end to avoid the collision.
>
> In my case, I saw millions of queries made to my site that look like
> this:
>
> "GET /function%20(filter,%20iter
> ator,%20context)%20%7B%20%20%20%20iterator%20=%20iterator%20?
> %20iterator.bind(context)%20:%20Prototype.K;%20%2
> 0%20%20var%20results%20=%20[];%20%20%20%20if%20(Object.isString
> (filter))%20{%20%20%20%20%20%20%20%20filter%20=
> %20new%20RegExp(filter);%20%20%20%20}%20%20%20%20this.each(function%20
> (value,%20index)%20{if%20(filter.match(v
> alue))%20{results.push(iterator(value,%20index));}});%20%20%20%20return
> %20results;} HTTP/1.1"
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to 
prototype-core-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to