简体   繁体   English

为什么JavaScript for循环中的迭代器不需要分号?

[英]Why does the iterator in a JavaScript for loop not need a semicolon?

I'm curious why when making a for loop in JavaScript a ; 我很好奇,为什么要在JavaScript中进行for循环? is not necessary after the i++ statement? i ++语句后没有必要吗? It tripped me up. 它使我绊倒了。 It actually doesn't even work if you include a semicolon after the iterator statement. 如果在iterator语句之后包含分号,则实际上甚至不起作用。

eg 例如

    for(var x=1; x<100; x++){

        document.write(x);
    }

While tokenizing the code for compiling, the JS-parser usually knows where statements start and end due to Automatic Semicolon Insertion . 在标记要编译的代码时,由于自动分号插入 ,JS分析器通常会知道语句在哪里开始和结束。

In the case of a for-loop however, semicolons are interpreted as a separator of statements. 但是, 对于for循环 ,分号被解释为语句的分隔符。 For-Loops accept up to three "parameters". 循环最多可以接受三个“参数”。 If you put in a semicolon, a fourth one will be expected and it will throw an error. 如果输入分号,则将使用第四个分号,这将引发错误。

The final-expression -statement of a for-loop is terminated by ) . for循环的final-expression- statement由)终止。

This control-structure behaves similar to PHP , C , Java and C++ 此控件结构的行为类似于PHPCJavaC ++

thanks to @Antoniu Livadariu for mentioning this in the comments 感谢@Antoniu Livadariu在评论中提到了这一点

Further Information 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM