这个for循环语法是什么意思?

huangapple go评论92阅读模式
英文:

What does this for loop syntax mean?

问题

I recently came to this thread, and had a quick question on the syntax used in the first answer. @ggorlen used this syntax/notation in the for loop that I've never seen before and couldn't find any answers online:

  for (;;) {
    try {
      await page.waitForFunction(
        `${thumbs.length} !==
          document.querySelectorAll("#video-title").length`, 
        {timeout: 10000}
      );
    }
    catch (err) {
      break;
    }

    thumbs = await page.$$eval("#video-title", els => {
      els.at(-1).scrollIntoView();
      return els.map(e => e.getAttribute("title"));
    });
  }

What does the for(;;) {...} do?

Thanks!

I just have a question on the syntax used, and couldn't find an answer.

英文:

I recently came to this thread, and had a quick question on the syntax used in the first answer. @ggorlen used this syntax/notation in the for loop that I've never seen before and couldn't find any answers online:

  for (;;) {
    try {
      await page.waitForFunction(
        `${thumbs.length} !==
          document.querySelectorAll("#video-title").length`, 
        {timeout: 10000}
      );
    }
    catch (err) {
      break;
    }

    thumbs = await page.$$eval("#video-title", els => {
      els.at(-1).scrollIntoView();
      return els.map(e => e.getAttribute("title"));
    });
  }

What does the for(;;) {...} do?

Thanks!

I just have a question on the syntax used, and couldn't find an answer.

答案1

得分: 1

for(;;) {
}

等同于

while (true) {
}

一个普通的 "for" 循环包含三个部分,用分号分隔,如 for(initialization; condition; afterthought)

初始化部分在循环开始之前运行。

条件部分在每次迭代开始时检查,如果评估为真,则执行循环体中的代码。

迭代之后的部分在每次循环迭代结束时执行。

允许省略 for 表达式的这些部分,例如 for(;;)。当省略时,实际上意味着它将永远循环,因为条件不存在,或者直到代码达到 returnbreak

您可以阅读更多关于 for 函数的信息,查看 [这个 MDN 页面][1]。


<details>
<summary>英文:</summary>

    for(;;) {
    }

is the equivalent of 

 

    while (true) {
     }

a normal &quot;for&quot; loop contains 3 parts separated by semi-colons like `for(initialization; condition; afterthought)`

The initialization runs before the looping starts. 

The condition is checked at the beginning of each iteration, and if it evaluates to true, then the code in the loop body executes.

The afterthought is executed at the end of each iteration of the loop.

It is allowed to ommit these parts of the for expression e.g. `for(;;)` 
When omitted it essentially means that it will loop forever since the condition is not present or until the code reaches a `return` or `break`.

You can read more about the `for` function checkout [this MDN page][1].


  [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for#optional_for_expressions

</details>



huangapple
  • 本文由 发表于 2023年1月9日 03:52:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定