admin管理员组文章数量:1401641
I'm converting some es5 into es6 and using eslint and e across the following
while(1){
// Do something
}
I'm getting a lint error
Unexpected constant condition no-constant-condition
But I'm not sure exactly what this does. It resides inside a for loop
for(i = 0; i< arr.length; i++) {
while(1) {
idx = _p.indexOf(arr[i], idx);
if (idx == -1) break;
}
}
But there was no documentation and unable to find online what exactly while(1) does and how to write an alternative to get rid of this lint error.
I'm converting some es5 into es6 and using eslint and e across the following
while(1){
// Do something
}
I'm getting a lint error
Unexpected constant condition no-constant-condition
But I'm not sure exactly what this does. It resides inside a for loop
for(i = 0; i< arr.length; i++) {
while(1) {
idx = _p.indexOf(arr[i], idx);
if (idx == -1) break;
}
}
But there was no documentation and unable to find online what exactly while(1) does and how to write an alternative to get rid of this lint error.
Share Improve this question asked Nov 3, 2017 at 11:06 user6885115user6885115 3-
while(1) will continuously run, until you
break
out of it. It's a short hand for DO-WHILE. So it says, while the idx does not equal -1, always run this loop. – Dan Commented Nov 3, 2017 at 11:08 -
while(1)
is just another dirty approach for continuity – B001ᛦ Commented Nov 3, 2017 at 11:09 - Possible duplicate of Why does ESLint trigger lint errors on while(true) using fibers? – user8016859 Commented Nov 3, 2017 at 11:37
5 Answers
Reset to default 2You could rewrite the code with a do ... while
loop with a check at the end, because that is what you do.
for (i = 0; i< arr.length; i++) {
do {
idx = _p.indexOf(arr[i], idx);
} while (idx !== -1)
}
A Loop
Instructions are repeated
n
times.
While Loop
while
needs a condition (a boolean value) to handle a loop. If the condition is true
it executes else it does nothing.
In this case the condition is 1
and gets cast to a true
.
function getBooleanValue(value) {
return !!value
}
console.log('1 gets cast to:', getBooleanValue(1))
console.log('0 gets cast to:', getBooleanValue(0))
Infinite Loop
A loop that never terminates.
Your loop is after the cast while(true)
. With other words do for ever.
Why this Rule
From the Docs:
A constant expression (for example, a literal) as a test condition might be a typo or development trigger for a specific behavior. For example, the following code looks as if it is not ready for production.
if (false) { doSomethingUnfinished(); }
if you want to disable it: /*eslint-disable no-constant-condition*/
while(1)
creates a loop that runs forever (never terminates) unless you explicitly stop it with the break
keyword. The loop will also stop if an error occurs during it's execution. These constructs can be very dangerous (crash your pc/browser) and that is why jslint warns about them.
An other way of writing this would be while(true)
or using for(;;)
(https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/for).
Link to docs
This rule disallows constant expressions in the test condition of: if, for, while, or do...while statement ?: ternary expression
Examples of incorrect code for this rule:
if (false) {
doSomethingUnfinished();
}
if (void x) {
doSomethingUnfinished();
}
for (;-2;) {
doSomethingForever();
}
while (typeof x) {
doSomethingForever();
}
do {
doSomethingForever();
} while (x = -1);
var result = 0 ? a : b;
Do: while(true)
Or you can just disable that: /*eslint-disable no-constant-condition*/
That's simply an infinite loop. It cycles until 1 isn't true (never). As you can see, inside this loop there's a condition and a break, so the infinite loop will end when the condition is true, hence the break.
while(1) { //Cycle forever...
idx = ...;
if (idx == -1) break; //...until idx == -1!
}
本文标签: ecmascript 6while(1) in JavaScriptStack Overflow
版权声明:本文标题:ecmascript 6 - while(1) in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744244398a2596939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论