<分区>
如何在 JavaScript 中构建循环?
标签 javascript loops
<分区>
如何在 JavaScript 中构建循环?
最佳答案
For 循环
for (i = startValue; i <= endValue; i++) {
// Before the loop: i is set to startValue
// After each iteration of the loop: i++ is executed
// The loop continues as long as i <= endValue is true
}
For...in 循环
for (i in things) {
// If things is an array, i will usually contain the array keys *not advised*
// If things is an object, i will contain the member names
// Either way, access values using: things[i]
}
使用 for...in
循环遍历数组是不好的做法。它违背了 ECMA 262标准,并且在将非标准属性或方法添加到 Array 对象时可能会导致问题,例如通过 Prototype .
(感谢 Chase Seibert 在评论中指出这一点)
While 循环
while (myCondition) {
// The loop will continue until myCondition is false
}
关于javascript - 如何在 JavaScript 中构建循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52080/