News for hackers. Stuff that matters.
Re:Nine Javascript Gotchas by nirmal on Thursday February 14, 2008 @02:33PM (#116020)
The error with the array just seems like poor practice. If you only want to deal with the contents of an array then you should use the [] with an integer index.
Re:Nine Javascript Gotchas by axod on Thursday February 14, 2008 @05:03PM (#116111)
I agree. You should NOT use 'for in' on an array. Secondly, if you use 'for in' on an object, the first line inside your loop will most usually be:

if (e.hasOwnProperty(key))

This gets rid of anything defined on the prototype etc and would have solved the issue in the article. However, using for...in on an Array isn't good practice anyway.

Re:Nine Javascript Gotchas by paul on Thursday February 14, 2008 @10:21PM (#116250)
You should NOT use any library that messes with Object.prototype or Array.prototype, then this won't be a problem and the for loops will work just fine.
Re:Nine Javascript Gotchas by axod on Friday February 15, 2008 @12:34PM (#116583)
Not particularly, if you're extending your own objects, you could still get properties in a for...in that you weren't expecting, so hasOwnProperty is always a good idea.

Regardless though, using for...in on an Array is bad practice and not a good idea. Just like having sparse Arrays in js is a bad idea.