Java enhanced for loop is very useful
Copy codeThe Code is as follows:
For (String str: list ){
System. out. println (str); // Where str is directly an element in the Set
}
However, the for/in loop provided for us in JavaScript is no longer that simple.
Copy codeThe Code is as follows:
Var car
Var garage = new Array ()
Garage [0] = "BMW"
Garage [1] = "Mercedes-Benz"
Garage [2] = "Bentley"
For (car in garage)
{
Document. write (garage [car] + "")
}
// Output result: BMW Benz Bentley
It looks like I got my car list
But now I have higher requirements for my garage. I hope it can be locked and self-cleaned.
Therefore
Copy codeThe Code is as follows:
Var car
Var garage = new Array ()
Garage [0] = "BMW"
Garage [1] = "Mercedes-Benz"
Garage [2] = "Bentley"
Garage. locked = true
Garage. clean = function (){
Alert ("clean ")
}
For (car in garage)
{
Document. write (garage [car] + "")
}
// Output result: BMW Benz Bentley true function () {alert ("clean ")}
Well, it says everything you know.
To avoid this embarrassment, we have to use the original for loop.
Copy codeThe Code is as follows:
Var car
Var garage = new Array ()
Garage [0] = "BMW"
Garage [1] = "Mercedes-Benz"
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 Benz Bentley
Well, this is much better.