The enhanced for loop in Java is easy to use
Copy Code code as follows:
for (String str:list) {
System.out.println (str);//Where STR is directly the element of the collection.
}
But the for/in loops that are provided for us in JavaScript are not that simple.
Copy Code code as follows:
var car
var garage= new Array ()
Garage[0] = "BMW"
GARAGE[1] = "Mercedes"
GARAGE[2] = "Bentley"
For (car in garage)
{
document.write (Garage[car] + "")
}
Output result: BMW Mercedes-Benz Bentley
Looks like I got a list of my cars.
But now I have higher requirements for my garage, and I want it to be locked and clean.
So
Copy Code code as follows:
var car
var garage= new Array ()
Garage[0] = "BMW"
GARAGE[1] = "Mercedes"
GARAGE[2] = "Bentley"
Garage.locked = True
Garage.clean = function () {
Alert ("clean")
}
For (car in garage)
{
document.write (Garage[car] + "")
}
Output result: BMW Mercedes-Benz Bentley True function () {alert ("clean")}
Well, it tells you everything you know.
To avoid this embarrassment, we had to use the original for loop.
Copy Code code as follows:
var car
var garage= new Array ()
Garage[0] = "BMW"
GARAGE[1] = "Mercedes"
GARAGE[2] = "Bentley"
Garage.locked = True
Garage.clean = function () {
Alert ("clean")
}
for (car = 0;car < garage.length;car++)
{
document.write (Garage[car] + "")
}
Output result: BMW Mercedes-Benz Bentley
Well, that's a lot better.