![]() |
|
|||
|
On Sep 26, 7:53*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote: > Jorge wrote: > > SAM wrote: > >> for(i in lista) { } > > > Hmm, what a lovely 'for..in' ! > > I had never applied it to an array. > > > javascript:a=['0',,,,,,,,'8'], a[4]= '4', n= 0;for (i in a) { n++; > > alert(i+a[i]); };alert(n+' !== '+a.length); > > > Is it predictable in this case ? the order of 'i', I mean ? > > No; read ES3F, 12.6.4. > That's what I thought, but as SAM said, however hard I try, it always shows them in the right order. -- Jorge. |
|
|||
|
On Sep 26, 2:38*pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote: > Le 9/25/08 7:14 PM, Jorge a écrit : > > > On Sep 24, 1:48 am, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid> > > wrote: > >> for(i in lista) { } > > > Hmm, what a lovely 'for..in' ! > > I had never applied it to an array. > > > javascript:a=['0',,,,,,,,'8'], a[4]= '4', n= 0;for (i in a) { n++; > > alert(i+a[i]); };alert(n+' !== '+a.length); > > > Is it predictable in this case ? the order of 'i', I mean ? > > What do you mean by 'predictable' ? > That the sequence grows orderly. But the spec states that properties might show up in any order. (See Thomas' ('Mr. spec') post...) -- Jorge. |
|
|||
|
On 2008-09-27 01:56, Jorge wrote:
> On Sep 26, 7:53 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> ... >> > javascript:a=['0',,,,,,,,'8'], a[4]= '4', n= 0;for (i in a) { n++; >> > alert(i+a[i]); };alert(n+' !== '+a.length); >> >> > Is it predictable in this case ? the order of 'i', I mean ? >> >> No; read ES3F, 12.6.4. >> > > That's what I thought, but as SAM said, however hard I try, it always > shows them in the right order. For one thing, there's this possibility: var a = ['0', '1']; a.blah = "ho hum"; a.push('2', '3'); for (var i in a) { document.write(i + ": " + a[i] + "<br>"); } Now I'm afraid I'll have to partly contradict what I wrote earlier in a different thread (alert vs. window.alert). In this specific case, I would *not* trust implementations to always return the results in the expected order, even though empirical evidence suggests that they currently do. The reason why I'm more reluctant here is that we're always warned (and that goes for various different programming languages) that hashes / associative arrays / objects should not be trusted to return their keys in any particular order when you iterate like that. This is a very common pitfall in many languages. Some do have ordered hashes (like the [weird] PHP arrays, which are actually a mix of arrays and hashes, or Perl's pseudohashes), but I'll bet my hat that Thomas can come up with a passage in the ECMAScript specs that says that object properties aren't guaranteed to be returned in any particular order in a for..in loop. In short, it may work. I can even imagine that the current script engines are designed to work that way, because too many badly written scripts would fail otherwise. But I wouldn't trust it. Iterate over an array like an array, and an object like an object. - Conrad |
|
|||
|
On Sep 26, 2:38*pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote: > Le 9/25/08 7:14 PM, Jorge a écrit : > > > On Sep 24, 1:48 am, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid> > > wrote: > >> for(i in lista) { } > > > Hmm, what a lovely 'for..in' ! > > I had never applied it to an array. > > > javascript:a=['0',,,,,,,,'8'], a[4]= '4', n= 0;for (i in a) { n++; > > alert(i+a[i]); };alert(n+' !== '+a.length); > > > Is it predictable in this case ? the order of 'i', I mean ? > > What do you mean by 'predictable' ? > > javascript:a=['0',,,,,,,,'8'], a[4]= '4';for(i in a) > if(a[i])alert('elem.t #'+i+' = '+a[i]); > When I run this in webkit r36882 the last (4th) alert reads: 'elem.t #peek = function () {return this[this.length-1]}' ??? What (the hell) is that ?? -- Jorge. |
|
|||
|
On Sep 27, 2:26*am, Conrad Lender <crlen...@yahoo.com> wrote:
> On 2008-09-27 01:56, Jorge wrote: > > > On Sep 26, 7:53 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> > .. > >> > javascript:a=['0',,,,,,,,'8'], a[4]= '4', n= 0;for (i in a) { n++; > >> > alert(i+a[i]); };alert(n+' !== '+a.length); > > >> > Is it predictable in this case ? the order of 'i', I mean ? > > >> No; read ES3F, 12.6.4. > > > That's what I thought, but as SAM said, however hard I try, it always > > shows them in the right order. > > For one thing, there's this possibility: > > var a = ['0', '1']; > a.blah = "ho hum"; > a.push('2', '3'); > for (var i in a) { > * * document.write(i + ": " + a[i] + "<br>"); > > } > Yes, but still sorts it well. This one instead... :-( javascript:a=[0,,2];a[1]=undefined;for (i in a) { alert(i+':'+a[i]) }; -- Jorge. |
|
|||
|
On 2008-09-27 03:04, Jorge wrote:
>> var a = ['0', '1']; >> a.blah = "ho hum"; >> a.push('2', '3'); >> for (var i in a) { >> document.write(i + ": " + a[i] + "<br>"); >> >> } > > Yes, but still sorts it well. For custom values of "well". It sorts "blah" between index 1 and 2 (FF2). > This one instead... :-( > > javascript:a=[0,,2];a[1]=undefined;for (i in a) { alert(i+':'+a[i]) }; I don't get it. What does this display in your browser? All it does in FF2 is alert: 0:0 1:undefined 2:2 Which is what you would expect, I guess. - Conrad |
|
|||
|
On Sep 27, 3:55*am, Conrad Lender <crlen...@yahoo.com> wrote:
> On 2008-09-27 03:04, Jorge wrote: > > >> var a = ['0', '1']; > >> a.blah = "ho hum"; > >> a.push('2', '3'); > >> for (var i in a) { > >> * * document.write(i + ": " + a[i] + "<br>"); > > >> } > > > Yes, but still sorts it well. > > For custom values of "well". > It sorts "blah" between index 1 and 2 (FF2). Ah, :-( then. > > This one instead... :-( > > > javascript:a=[0,,2];a[1]=undefined;for (i in a) { alert(i+':'+a[i])}; > > I don't get it. What does this display in your browser? > All it does in FF2 is alert: > > * 0:0 > * 1:undefined > * 2:2 > > Which is what you would expect, I guess. > No, see, here a[1] === undefined as well, but it's not listed : javascript:a= [0,,2];for (i in a) { alert(i+':'+a[i]) }; I was thinking that the for..in would help in transversing a sparse array (an array with 'holes'), but that's not the case. -- Jorge. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|