no-labels
禁止带标签的语句
JavaScript 中的标记语句与 break
和 continue
结合使用来控制多个循环的流程。例如:
outer:
while (true) {
while (true) {
break outer;
}
}
break outer
语句确保此代码不会导致无限循环,因为在应用 outer
标签后控制权返回到下一条语句。如果将这条语句更改为只是 break
,控制将流回外部 while
语句,并导致无限循环。
虽然在某些情况下很方便,但标签往往很少使用,并且被一些人认为是一种更容易出错且更难理解的流量控制的补救形式。
规则详情
该规则旨在消除 JavaScript 中标记语句的使用。每当遇到带标签的语句以及 break
或 continue
与标签一起使用时,它都会发出警告。
7jd2bhXiUGDmBKu+cZeo8q9FHH9BbCCMY2Txw3OX9o2XrFfZEB7Ku3Dcx85/CBwT
/*eslint no-labels: "error"*/
label:
while(true) {
// ...
}
label:
while(true) {
break label;
}
label:
while(true) {
continue label;
}
label:
switch (a) {
case 0:
break label;
}
label:
{
break label;
}
label:
if (a) {
break label;
}
NwldEOIuHKyv0R+3pkUiuSlq7yzV1WsYiQ97qEISbUzf2ki35mZ0HV/HKFTSzvSb
/*eslint no-labels: "error"*/
var f = {
label: "foo"
};
while (true) {
break;
}
while (true) {
continue;
}
选项
Z4w/O+pwF4+HjquIxU1nAPl28RPu60TsoZZ+HWYo4eb65F5Jo8w9ox7E/1VG5yQsdwLLYlUyo3FQLbpYMAqMyw==
- M0jWid4l/TMNPuQonuMMcy8oPj4WVzEiNTZXBZguMTphQG66Pz5IL1BwOQrbSLPwt4IqYWq09HyIoCgqaeX3F34gugkQc6hPj4kc7Hqcq+94yKxL3bYy7dsePgozJSvV2rvBiOh9inAKv2A9ybGKvquM6PgC3K14YrqkihE7f3dnuy1YrmbCVaDUYep6LTpdqkyGho5q1L18rK7BAp/vpm2736ojnFNBV9VfG2baMIRgKN2eBfLCQooRkJZCJ7A598oX/E7M/EmE1RrgGXbrEVyDQekFNcHKyqTLsweOXyr/Xg8cWI7bB/LP4Xlof+XEePKqrxMqEE8NfNWt6xdkU4Gj7ZNkrWrrPvdVwsDP2q4NqURkUt0HRgs22gP0On8Ob0cieawklIzMPWn5uReHL/Xmz2l2Jqtn/CZHpQtDBtorppEpxu3QACJl8qYJCRK41t7H5eIzebxUuZgM3Z6vSkxAtpYdH7FF8ROq4e3eF/4R8HMGTfY0swwmsk6ucJNfP4XZIEw81XqC9eGKMdyx+w==
Y3CLaBr/qRgcseS2HDv+wRdY4EeLFhmIw/Z45Hv5OFzdro874bzLgROV8TNjEAPdiwjA8Ke6vBAGufm24QKkO6Ui4lDKkvo2oixAZqhi/MHsoj+zdfKpkXOkjb5PzLdIF76pwGcvJCi+ngoZM8Z4nWstZENyQOET/jFYjbl9rnWh/IZczYRKqK7TlKSfLV6XUYadgNnVj8HogMOAJXqhEA1L0/iC65xs9EBS7mbR+L8YmXeCR0gQoZOrcBhbpPHh
allowLoop
H3vhaQU3NTT9XG4zK6+cENwsBJdZM5jEoO1eY1DZE6fF2DbeBEGArBnm7j0jc+zKxXIDZSLDJQ85yy6BCno1lOD5zWqZ0H85mHFozUXIuaE=
/*eslint no-labels: ["error", { "allowLoop": true }]*/
label:
while (true) {
break label;
}
allowSwitch
cJld7bnvOpNAlXv9SkBZPA3RamPCEls2RCdTxloM0ZeyLOeGUCFyYPP5ffwU5eI6w5GruS8ggQmASsA0w9ppI9XWZziwpIbyQYWrOJB6nWs=
/*eslint no-labels: ["error", { "allowSwitch": true }]*/
label:
switch (a) {
case 0:
break label;
}
何时不使用
4R5+q8wZy26N/OUdkMz7ZC86Jb0hkne4Gl9YqTiGzi8e/vWgTMe8DjjFjSIyL1F3WqRej00XdEVhJ8u7EUij5VZYAa7HuWDpJz3soQlrEpYkNcHidILFRDCPFwMk+PQdNzCHVZ19bQjOFVXGGzylGQ==