no-continue

禁止 continue 语句

continue 语句终止当前或标记循环的当前迭代中语句的执行,并在下一次迭代中继续执行循环。如果使用不当,它会降低代码的可测试性、可读性和可维护性。应该使用结构化的控制流语句,例如 if

¥The continue statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration. When used incorrectly it makes code less testable, less readable and less maintainable. Structured control flow statements such as if should be used instead.

var sum = 0,
    i;

for(i = 0; i < 10; i++) {
    if(i >= 5) {
        continue;
    }

    sum += i;
}

规则详情

¥Rule Details

此规则不允许 continue 语句。

¥This rule disallows continue statements.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-continue: "error"*/

var sum = 0,
    i;

for(i = 0; i < 10; i++) {
    if(i >= 5) {
        continue;
    }

    sum += i;
}
在线运行
/*eslint no-continue: "error"*/

var sum = 0,
    i;

labeledLoop: for(i = 0; i < 10; i++) {
    if(i >= 5) {
        continue labeledLoop;
    }

    sum += i;
}

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-continue: "error"*/

var sum = 0,
    i;

for(i = 0; i < 10; i++) {
    if(i < 5) {
       sum += i;
    }
}

兼容性

¥Compatibility

  • JSLint:continue

版本

此规则是在 ESLint v0.19.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号