Programming + Design

Why Javascript Arrays Suck PDF Print E-mail
Written by Brett Brewer   
Thursday, 24 April 2008
I've been writing a new AJAX based POS system for a client and lamenting the horrible state of javascript array manipulation which has consumed the bulk of my time. It seems that there is a major problem with trying to iterate over arrays with arbitrary numerical indexes. The problem is, there's no good way to iterate over a javascript array to achieve a typical "foreach" type of loop such as what you would commonly do in PHP. One of the best uses for this is to gain access to the name or number of the index for the array element you are currently iterating over so you can use the array index in some kind of programming logic. To achieve this same functionality in Javascrtip, most people use ( loops to "enumerate" the properties of an array. This works if you have created a "sparse array" that contains only numeric indexes, but the problem is that Javascript library creators (Scriptaculous, Prototype, and many others) modify the Array prototype at runtime and add all sorts of properties that get added to your array and end up being enumerated in your loop. This tends to break the hell out of legacy javascript code that relies on to loop over an array. Not cool. Javascript gurus will tell you this is normal behavior and that's what a loop is for, but lazy programmers like me will tell you this sucks because it makes something that should be extremely simple, extremely difficult. Anyway, this article on DHTML Kitchen explains it better than I do. If you're trying to iterate over a Javascript array like you do your PHP arrays, read the article to see why your life is such hell.
Last Updated ( Thursday, 24 April 2008 )
< Prev


Who's Online

We have 1 guest online

© 2017
Joomla! is Free Software released under the GNU/GPL License.