Enhanced for loops in Java are very useful
for (String str : list) {
(str);// where str is directly the element in the collection
}
But the for/in loop provided for us in JavaScript is no longer that simple
var car
var garage= new Array()
garage[0] = "BMW"
garage[1] = "Mercedes-Benz"
garage[2] = "Bentley"
for (car in garage)
{
(garage[car] + " ")
}
//Output result: BMW Mercedes-Benz Bentley
It looks like I've got my car list
But now I have higher demands on my garage and I want it to be locked and cleaned on its own
then
var car
var garage= new Array()
garage[0] = "BMW"
garage[1] = "Mercedes-Benz"
garage[2] = "Bentley"
= true
= function(){
alert("clean")
}
for (car in garage)
{
(garage[car] + " ")
}
//Output result: BMW Mercedes-Benz Bentley true function (){ alert("clean") }
OK, it said everything it knows
In order to avoid this embarrassment, we had to use the original for loop to return to the original for loop
var car
var garage= new Array()
garage[0] = "BMW"
garage[1] = "Mercedes-Benz"
garage[2] = "Bentley"
= true
= function(){
alert("clean")
}
for (car = 0;car < ;car++)
{
(garage[car] + " ")
}
//Output result: BMW Mercedes-Benz Bentley
Yes, it's much better now.
Copy the codeThe code is as follows:
for (String str : list) {
(str);// where str is directly the element in the collection
}
But the for/in loop provided for us in JavaScript is no longer that simple
Copy the 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)
{
(garage[car] + " ")
}
//Output result: BMW Mercedes-Benz Bentley
It looks like I've got my car list
But now I have higher demands on my garage and I want it to be locked and cleaned on its own
then
Copy the codeThe code is as follows:
var car
var garage= new Array()
garage[0] = "BMW"
garage[1] = "Mercedes-Benz"
garage[2] = "Bentley"
= true
= function(){
alert("clean")
}
for (car in garage)
{
(garage[car] + " ")
}
//Output result: BMW Mercedes-Benz Bentley true function (){ alert("clean") }
OK, it said everything it knows
In order to avoid this embarrassment, we had to use the original for loop to return to the original for loop
Copy the codeThe code is as follows:
var car
var garage= new Array()
garage[0] = "BMW"
garage[1] = "Mercedes-Benz"
garage[2] = "Bentley"
= true
= function(){
alert("clean")
}
for (car = 0;car < ;car++)
{
(garage[car] + " ")
}
//Output result: BMW Mercedes-Benz Bentley
Yes, it's much better now.