javascript - 为什么这个 'for(;;)' 循环?

标签 javascript loops infinite-loop

到底是什么让第二个参数返回 true

WARNING: it will loop infinitely and might crash your browser

for(;;){
    //...
}

我完全没想到会循环...

但它正在运行,这让情况变得更糟,因为它只能在评估为 true 的情况下运行,或者我是否遗漏了什么?

最佳答案

来自 forMDN

for ([initialization]; [condition]; [final-expression])
   statement

[Initialization][final-expression] 直觉上不是必需的,将构造简化为简单的 while 循环。但相关部分是:

condition
An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.

(强调我的)

这似乎是一个完全任意的 JS 语言设计决定。如果是我的语言,我可能会抛出语法错误。


有趣的是,for(;;){} 等同于 while(true){},恰好短了 4 个字符。我想知道缩小器是否利用了这一点!

关于javascript - 为什么这个 'for(;;)' 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9936578/

相关文章:

javascript - typescript :正则表达式不匹配预期的模式

javascript - 如何在提交按钮上同时使用 dvloading 和 required

javascript - 循环遍历对象直到找到参数(VueJS)

jQuery 无限循环遍历每个 div

java - 从方法读取文件时出现无限循环

javascript - 如何删除对象?

javascript - 解决所有错误后,如何使 data-parsley-errors-container ="#element"消失?

javascript - 循环递归?

jquery - MySQL 中遍历行并删除一些数据的代码是什么?

assembly - 不延迟汇编的循环会使我的电脑崩溃吗?