英文:
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(;;)
。当省略时,实际上意味着它将永远循环,因为条件不存在,或者直到代码达到 return
或 break
。
您可以阅读更多关于 for
函数的信息,查看 [这个 MDN 页面][1]。
<details>
<summary>英文:</summary>
for(;;) {
}
is the equivalent of
while (true) {
}
a normal "for" 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论