Am 11.03.2011, 21:58 Uhr, schrieb Alex <[email protected]>:
I'm struggling to find something very simple.
Question 1:
If I have an array of integers and I use a for-in construct, do I get
them in the same order they are present inside array?
ex:
var v = [1,2,3,4]
for(var i in v){
print(i + '-->' + v[i]);
}
is this similar to
for(var i=0;i<v.length; i++){
print(i + '-->' + v[i]);
}
From what I know (and saw in ECMA 262) for-in returns the properties
of an object and does not guarantee the order of the properties....so
the answer seems to be "no" - those 2 constructs are not equivalent.
(from this point of view)
Question 2:
If you can use both first and second construct does it means that the
indices of the vector are both properties (of object Array) and
indices of the array?
What you also would have to consider is that the type of i in the for-loop
is a number, whereas in the for-in-loop it's a string.
The general problems with a for-in-loop over arrays were mentioned by
Peter.
Matt
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]