问题
anyone can explain how to use for...in statement in javascript. I had read the w3school article but i think it is not so clear.Below is the code, please explain this:
<html>
<body>
<script type="text/javascript">
var x;
var mycars = new Array();
mycars[10] = "Saab";
mycars[20] = "Volvo";
mycars[30] = "BMW";
for (x in mycars)
{
document.write(mycars[x] + "<br />");
}
</script>
</body>
</html>
回答1:
A for in loop will iterate through every property in an object.
In your example, the x
variable will cycle through every property in the mycars
object.
If you add mycars.expensive = "Porsche";
, it will find that too.
Note that, as stated by MDC, for in
loops should not be used to loop through ordinary arrays:
Although it may be tempting to use this as a way to iterate over an Array, this is a bad idea. The
for...in
statement iterates over user-defined properties in addition to the array elements, so if you modify the array's non-integer or non-positive properties (e.g. by adding a"foo"
property to it or even by adding a method or property toArray.prototype
), thefor...in
statement will return the name of your user-defined properties in addition to the numeric indexes. Also, because order of iteration is arbitrary, iterating over an array may not visit elements in numeric order. Thus it is better to use a traditional for loop with a numeric index when iterating over arrays. Similar arguments might be used against even using for...in at all (at least without propertyIsEnumerable() or hasOwnProperty() checks), since it will also iterate over Object.prototype (which, though usually discouraged, can, as in the case of Array.prototype, be usefully extended by the user where are no namespacing concerns caused by inclusion of other libraries which might not perform the above checks on such iterations and where they are aware of the effect such extension will have on their own use of iterators such as for...in).
回答2:
First you create an object with 3 items (and not an Array)
var mycars = new Object();
mycars[10] = "Saab";
mycars[20] = "Volvo";
mycars[30] = "BMW";
where 10
, 20
and 30
are the object properties.
then you want to navigate through the object, visit all properties and display each value associated to a property.
This is where the [ for
(variable in
object) expression ] javascript construction intervenes:
The variable will be set to the first property of the object, then to the 2nd, then to the last. Try
for (v in mycars) alert(v);
to see how it works, and this as well
for (v in mycars) alert("Property: "+v+", value: "+mycars[v]);
回答3:
The for ... in
construction iterates over every element within the object on the right side of in
. In your case, the block below the for
statement is executed once for every car in mycars
.
回答4:
for in
is a way of burying bugs for later generations to discover. As has been copiously pointed out, if applied to an Array, it will loop through all the elements of the array, and the length of the array, and whatever other data happens to be attached to the array. Applying it to an ordinary object is better, but you still should still filter the indices through hasOwnProperty
.
Better to use a framework-provided function like jQuery's $.each
来源:https://stackoverflow.com/questions/4559491/what-is-for-in-statement-in-javascript