<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>14901</bug_id>
          
          <creation_ts>2007-08-08 02:53:52 -0700</creation_ts>
          <short_desc>for in loop on an Array does not work</short_desc>
          <delta_ts>2008-09-18 01:54:23 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>419.x</version>
          <rep_platform>Mac</rep_platform>
          <op_sys>OS X 10.4</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>INVALID</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>0</everconfirmed>
          <reporter name="Andrea Trasatti">atrasatti</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ap</cc>
    
    <cc>mitz</cc>
    
    <cc>wesc</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2829</commentid>
    <comment_count>0</comment_count>
    <who name="Andrea Trasatti">atrasatti</who>
    <bug_when>2007-08-08 02:53:52 -0700</bug_when>
    <thetext>I created a test Array with 3 elements, tried to cycle with a for in loop. Safari simply does not enter the loop.

This is a sample script I wrote:
var mycars=new Array();
mycars[0]=&apos;Saab&apos;;
mycars[1]=&apos;Volvo&apos;;
mycars[2]=&apos;BMW&apos;; &quot;.
for (i=0;i&lt;mycars.length;i++) {
        alert(&apos;car maker id&apos;+i+&apos;, name &apos;+mycars[i]);
}</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2830</commentid>
    <comment_count>1</comment_count>
    <who name="Andrea Trasatti">atrasatti</who>
    <bug_when>2007-08-08 03:07:27 -0700</bug_when>
    <thetext>(In reply to comment #0)
Obviously the submitted code sample was wrong.

What I wanted to submit as a sample is:
var mycars=new Array();
mycars[0]=&apos;Saab&apos;;
mycars[1]=&apos;Volvo&apos;;
mycars[2]=&apos;BMW&apos;;
for (var i in mycars) {
        alert(&apos;car maker id&apos;+i+&apos;, name &apos;+mycars[i]);
}


</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2833</commentid>
    <comment_count>2</comment_count>
      <attachid>15867</attachid>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2007-08-08 06:29:36 -0700</bug_when>
    <thetext>Created attachment 15867
test (works for me)

Same test in an attachment.

Seems to work fine in shipping 10.4.10 Safari/WebKit, and also with nightly r24875.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2835</commentid>
    <comment_count>3</comment_count>
      <attachid>15868</attachid>
    <who name="Andrea Trasatti">atrasatti</who>
    <bug_when>2007-08-08 07:06:45 -0700</bug_when>
    <thetext>Created attachment 15868
test that does not work for me</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2836</commentid>
    <comment_count>4</comment_count>
    <who name="Andrea Trasatti">atrasatti</who>
    <bug_when>2007-08-08 07:08:23 -0700</bug_when>
    <thetext>(In reply to comment #2)
&gt; Created an attachment (id=15867) [edit]
&gt; test (works for me)
&gt; 
&gt; Same test in an attachment.
&gt; 
&gt; Seems to work fine in shipping 10.4.10 Safari/WebKit, and also with nightly
&gt; r24875.

It is working for me too, actually. Weird how I could not make it work in my page and then created a sample script that worked.

The original reason why I filed the bug was this:
var children = document.getElementsByTagName(&apos;SELECT&apos;);
alert(children.length);
for (var c in children) {
   alert(&apos;loop &apos;+c);
}


I left the alerts just for your reference. In my page I have 5 select elements and the first alert correctly says so, but then never gets to the &quot;loop*&quot; alerts.
See the new attachment that I have created. I made two versions one that is within a function and one (less readable) directly in the select element.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2837</commentid>
    <comment_count>5</comment_count>
    <who name="">mitz</who>
    <bug_when>2007-08-08 07:24:48 -0700</bug_when>
    <thetext>(In reply to comment #4)
&gt; var children = document.getElementsByTagName(&apos;SELECT&apos;);
&gt; alert(children.length);
&gt; for (var c in children) {
&gt;    alert(&apos;loop &apos;+c);
&gt; }

The result of getElementsByName is not an array, it is a NodeList. for-in enumerates properties of the NodeList object. To enumerate the nodes in the NodeList you can do something like
for (var i = 0; i &lt; children.length; i++)
    alert(&apos;loop&apos; + children[i]);</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2841</commentid>
    <comment_count>6</comment_count>
    <who name="Andrea Trasatti">atrasatti</who>
    <bug_when>2007-08-08 08:04:40 -0700</bug_when>
    <thetext>(In reply to comment #5)
&gt; (In reply to comment #4)
&gt; &gt; var children = document.getElementsByTagName(&apos;SELECT&apos;);
&gt; &gt; alert(children.length);
&gt; &gt; for (var c in children) {
&gt; &gt;    alert(&apos;loop &apos;+c);
&gt; &gt; }
&gt; 
&gt; The result of getElementsByName is not an array, it is a NodeList. for-in
&gt; enumerates properties of the NodeList object. To enumerate the nodes in the
&gt; NodeList you can do something like
&gt; for (var i = 0; i &lt; children.length; i++)
&gt;     alert(&apos;loop&apos; + children[i]);
&gt; 

True, nevertheless for-in works fine on MSIE7 and FF2. Isn&apos;t the NodeList enumerated? Why would for work and for-in not?
It seems to me like the suggested loop, which is actually the exact same thing I did to fix it with Safari, should behave the same way as a for-in, while the for-in has a more compact syntax and is more readable, in this case.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>91509</commentid>
    <comment_count>7</comment_count>
    <who name="Wes Contreras">wesc</who>
    <bug_when>2008-09-17 12:44:27 -0700</bug_when>
    <thetext>I&apos;m working with Chrome (beta) and having a similar issue with for/in and arrays. Here is my test script:

var myarray = [5,3,8,&quot;test&quot;,&quot;nowork&quot;];
for (var elem in myarray) {
  document.write(&quot;&lt;br&gt;&quot; + elem);
}

Here is my output:

0
1
2
3
4

It appears to be outputting the array keys instead of the array values. The following code works perfectly, but for my solution, I can&apos;t count on contiguous  array elements, so this solution requires a lot more logic than a simple for/in loop.

for (var ii = 0; ii &lt; myarray.length; ii++) {
  document.write(&quot;&lt;br&gt;&quot; + myarray[ii]);
}</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>91600</commentid>
    <comment_count>8</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2008-09-18 01:54:23 -0700</bug_when>
    <thetext>(In reply to comment #6)
&gt; True, nevertheless for-in works fine on MSIE7 and FF2. Isn&apos;t the NodeList
&gt; enumerated? Why would for work and for-in not?

I think that this was a bug in WebKit shipping with Safari 2, fixed in Safari 3. However, it&apos;s still a bad idea to use for-in with NodeLists. For-in enumerates all properties of NodeList, so you get not only loop 0...loop 4, but also loop length and loop item. This is rarely what is needed.

(In reply to comment #7)
&gt; 0
&gt; 1
&gt; 2
&gt; 3
&gt; 4

I&apos;m getting identical results in Firefox.

Since this bug doesn&apos;t contain any issues identified as bugs, I&apos;m closing it as INVALID. Please feel free to open new bugs if you disagree - this one contains a lot of confusion between arrays and NodeLists, so it would be better to avoid continuing the discussion here.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>15867</attachid>
            <date>2007-08-08 06:29:36 -0700</date>
            <delta_ts>2007-08-08 06:29:36 -0700</delta_ts>
            <desc>test (works for me)</desc>
            <filename>forin.html</filename>
            <type>text/html</type>
            <size>176</size>
            <attacher name="Alexey Proskuryakov">ap</attacher>
            
              <data encoding="base64">PHNjcmlwdD4KdmFyIG15Y2Fycz1uZXcgQXJyYXkoKTsKbXljYXJzWzBdPSdTYWFiJzsKbXljYXJz
WzFdPSdWb2x2byc7Cm15Y2Fyc1syXT0nQk1XJzsKZm9yICh2YXIgaSBpbiBteWNhcnMpIHsKICAg
ICAgICBhbGVydCgnY2FyIG1ha2VyIGlkJytpKycsIG5hbWUgJytteWNhcnNbaV0pOwp9Cjwvc2Ny
aXB0Pgo=
</data>

          </attachment>
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>15868</attachid>
            <date>2007-08-08 07:06:45 -0700</date>
            <delta_ts>2007-08-08 07:06:45 -0700</delta_ts>
            <desc>test that does not work for me</desc>
            <filename>test.html</filename>
            <type>text/html</type>
            <size>1731</size>
            <attacher name="Andrea Trasatti">atrasatti</attacher>
            
              <data encoding="base64">PCFET0NUWVBFIGh0bWwgUFVCTElDICItLy9XM0MvL0RURCBYSFRNTCAxLjAgU3RyaWN0Ly9FTiIg
Imh0dHA6Ly93d3cudzMub3JnL1RSL3hodG1sMS9EVEQveGh0bWwxLXN0cmljdC5kdGQiPgo8aHRt
bCB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94aHRtbCIgeG1sOmxhbmc9ImVuIiBsYW5n
PSJlbiI+CgkgIDxoZWFkPgoJCSAgICAgIDx0aXRsZT5UZXN0PC90aXRsZT4KCQkJCTxzY3JpcHQ+
CgkJCQkJZnVuY3Rpb24gZm9yaW5fbG9vcCgpIHsKCQkJCQkJdmFyIGNoaWxkcmVuID0gZG9jdW1l
bnQuZ2V0RWxlbWVudHNCeVRhZ05hbWUoJ1NFTEVDVCcpOwoJCQkJCQlhbGVydChjaGlsZHJlbi5s
ZW5ndGgpOwoJCQkJCQlmb3IgKHZhciBjIGluIGNoaWxkcmVuKSB7CgkJCQkJCQlhbGVydCgnbG9v
cCAnK2MpOwoJCQkJCQl9CgkJCQkJfQoJCQkJPC9zY3JpcHQ+CgkgIDwvaGVhZD4KCSAgPGJvZHk+
CgkJICA8Zm9ybSBhY3Rpb249Ii9zYXZlIiAgbWV0aG9kPSJwb3N0IiBpZD0ibm9kZS1mb3JtIiBl
bmN0eXBlPSJtdWx0aXBhcnQvZm9ybS1kYXRhIj4KCQkJICA8ZGl2IGNsYXNzPSJmb3JtLWl0ZW0i
PgoJCQkJICA8c2VsZWN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