javascript - 如何在 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/

相关文章:

javascript - javascript中单引号转双引号,以及双引号转单引号

javascript - IE11 中的 getElementById(id).remove() 出错

javascript - 循环数组和另一个对象中的对象

python - 在Python中通过字符串制作A ROT13

c++ - 重叠计算

javascript - jQuery:如何获取选择数组中元素的索引?

javascript - 加载新数据时更新上下文行 d3

javascript - 以编程方式触发 jQuery-UI 可拖动的 "drag"事件

javascript - 如何检查一个键值对(在一个数组中)是否存在于另一个数组中?

java - 接受一系列数字? for循环