no-plusplus
禁止一元运算符 ++
和 --
因为一元 ++
和 --
运算符会自动插入分号,所以空格的差异会改变源代码的语义。
var i = 10;
var j = 20;
i ++
j
// i = 11, j = 20
var i = 10;
var j = 20;
i
++
j
// i = 10, j = 21
规则详情
此规则不允许一元运算符 ++
和 --
。
此规则的错误代码示例:
/*eslint no-plusplus: "error"*/
var foo = 0;
foo++;
var bar = 42;
bar--;
for (i = 0; i < l; i++) {
return;
}
此规则的正确代码示例:
/*eslint no-plusplus: "error"*/
var foo = 0;
foo += 1;
var bar = 42;
bar -= 1;
for (i = 0; i < l; i += 1) {
return;
}
选项
VtL0QjEmIoPGGZlPIOT+lHJvS1I3iz4tkRMrlBEMYEjqkAmYbAoIMmWhq1OHxJs7
- ZoK5QQYs4d64QXQdqnl7IIJo+nIbdDRQfHkoLRitdqcxNp7R6EtO8UeVzR7ibymCYN8pHuFOUjMfxzsccrR1SLSV4OyJMCgiQohrPGBJ7iauF7iEfzgLgvfLeXoNgyVpRUAL5uV6iS5WfznybC/44HIDtU9iPWNcn5PeyxD6kWLe5FGy3v46avrL+FAuYzxPEYrdFrovL6/TJHJLYJqNCQr+5M+khWAIZjnOMucqosdS3/F0PKeTsmHNVnrTL9SV
allowForLoopAfterthoughts
TlLpo7HIJjIy2v3f96HaCWHv9ZNzOkELx917A7qaUY9uiom7iLhWxeYjFu0EcYpiGTxf2gRoNqk76zN51r0ESAOzK0b3u+MqCRaTDJ3QHvNGlzVc9tpuES9hr9FQUruVSqC+tVHZlRW3C4ZFl5wQVA==
/*eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }]*/
for (i = 0; i < l; i++) {
doSomething(i);
}
for (i = l; i >= 0; i--) {
doSomething(i);
}
for (i = 0, j = l; i < l; i++, j--) {
doSomething(i, j);
}
TlLpo7HIJjIy2v3f96HaCWHv9ZNzOkELx917A7qaUY9uiom7iLhWxeYjFu0EcYpiGTxf2gRoNqk76zN51r0ESNkKnGnAn6f6Qti1XaMOdqBjwI81jkGv0XGmWcPcQPNvzoftxdh5DZDDibV4jU1tEw==
/*eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }]*/
for (i = 0; i < l; j = i++) {
doSomething(i, j);
}
for (i = l; i--;) {
doSomething(i);
}
for (i = 0; i < l;) i++;