Andreas Junghans wrote:
> Hi there,
> 
> Am 18.01.2009 um 18:45 schrieb Derrell Lipman:
> 
>> On Sun, Jan 18, 2009 at 11:55 AM, Mustafa Sak <m....@ims24-net.de>  
>> wrote:
>>> Hi,
>>>
>>> it is possible, that IE puts automaticaly Elments in to a new Array  
>>> by default?
>>>
>>> i tried this
>>>
>>> var xx = new Array();
>>> for (key in xx)
>>> {
>>>    this.debug(key);
>>> }
>>>
>>> In Firefox, i have no keys, like expected
>>>
>>> but at IE 6 i have this output:
>>>
>>> every
>>> some
>>> map
>>> filter
>>> forEach
>>> lastIndexOf
>>> indexOf
>>>
>>> Could this happen? Is there a work arround?
>> qooxdoo adds these functions to the Array prototype to provide a  
>> consistent interface between browsers. Although it seems to work to  
>> iterate an Array use for/in, it's not technically legal.
> 
> Sorry, I disagree. It's perfectly legal to iterate over an array with  
> for ... in. In fact, it can be very useful to do so if you're working  
> with sparse arrays (where only a few indices are ever assigned a  
> value). Please keep in mind that there's not a lot that separates a  
> JavaScript Array instance from any arbitrary object (which is always  
> an associative array in JavaScript):
> 
> 1.) Arrays have additional methods (like push()).
> 
> 2.) For all _numeric_ keys that are used, the length property is set  
> to the value of the key + 1, provided the previous length value was  
> lower.
> 
> Consider the following example, which only works correctly in a non- 
> qooxdoo environment:
> 
>      var x = new Array();
>      x[0] = 42;
>      for (var key in x) {
>          alert(typeof(key));    // result: string
>      }
>      x[2] = 43;
>      alert(x.length);    // result: 3
> 
> The results make it pretty clear that arrays in JavaScript are not  
> that special (unlike in other programming languages). They're  
> basically associative arrays - and every key is automatically  
> converted to a string! -, and the length property is pretty "dumb".
> 
> This behaviour is also described in the ECMA spec, so it's not just a  
> random decision made by the browser developers. Yes, it can be  
> confusing, especially for JavaScript newcomers, but that's the way  
> this part of the language works.
> 
>> That paradigm is for objects, not arrays. Instead you should use  
>> indexes:
>>
>>   for (var i = 0; i < xx.length; i++)
>>   {
>>     this.debug(i + ": " + xx[i]);
>>   }
> 
> As mentioned above, it can also be useful to iterate over an array  
> using for ... in. I think adding convenience methods to arrays is a  
> big mistake, even if they're present in other browsers. It can totally  
> screw array handling in some cases, and the benefits compared to  
> global functions or static methods are minimal (e.g. you could also  
> use indexOf(arrayInstance, obj) instead of arrayInstance.indexOf(obj)).
> 
> IMHO, the additional convenience you get by adding these methods to  
> the Array prototype are just not worth screwing with a core language  
> element (even if other browsers offer them natively, which means they  
> don't appear in for ... in iterations there).
> 
> Just my 2 cents,
> 
>    Andreas J.

Hi Andreas

You're right about all you said. However, since JavaScript is a
prototype-based language, you may sooner or later encounter modified
core language elements. Not only the qooxdoo framework does this...
there are numerous people who use prototype based extensions in JavaScript.

That being said, i think the point is valid that you should use "for ..
in" on objects or "associative-arrays" and use the for-loop on numeric
arrays.

Your example (sparse-array) would work just as well with "new Object()"
instead of "new Array()" and therefore we can come to the conclusion
that you should/could use "Array" for numeric, sequential Arrays and
"Object" for Maps, Associative-Arrays or whatever you like to call them.
At least in the qooxdoo context it's a good idea to think that way.

And as a side effect: This also helps you to distinguish between numeric
and associative arrays (as a human, but also programmatically).

Regards
- Roman

------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
qooxdoo-devel mailing list
qooxdoo-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Reply via email to