no-undef-init
不允许将变量初始化为 undefined
一些该规则报告的问题可以通过 --fix 命令行选项 自动修复
在 JavaScript 中,已声明但未初始化为任何值的变量会自动获取 undefined
的值。例如:
var foo;
console.log(foo === undefined); // true
因此不必将变量初始化为 undefined
,例如:
var foo = undefined;
避免将变量初始化为 undefined
被认为是最佳实践。
规则详情
38aBKK+TZtNeynLGAl+qZ257CG2B6gusajK6cyMlRK36mWG30mHln4YbB1C0xo0o9UDURb39mTOnFJO3a8XmAmoJY4aHNnz+pZlQn1PV5mjkDQJsvGakzS0PSwDC7cMjlN+93qDl6Z59b821DGyHSl1t6W/KDQx4Pqj25kND5Vg=
QnwhPm/cyTBJK+3dwgoJPe3E20un+2KP2CiFhQ+78TgFiUScrcmb3urZ1LYB2VeR
/*eslint no-undef-init: "error"*/
var foo = undefined;
let bar = undefined;
ZV3WTnsA6mjxcovKoGG8hTv7+Ha1z2USrMCyxcmIv+4859h0MFN8WvPgRtljmRMU
/*eslint no-undef-init: "error"*/
var foo;
let bar;
+FBnX08kHrhNIbPSeYOAlZphU4xKbJr3jhXWRS0UnMeo8UOu8iJfdOEAgFo9Myebb3iuxhxqDy8SL8GIsYpDXxkn6o5t5NytYO3BtDhe/03+6HvL7gEIBUujpNwTedBexClrRYSyLH92d+ijlpAGbw==
FC+RSdmeooMDvAC8f0gSEddiBAaEMR2/ycXuzoNq0VRdhPVRiEJH+l9wvo26/vum
/*eslint no-undef-init: "error"*/
const foo = undefined;
let { bar = undefined } = baz;
[quux = undefined] = quuux;
(foo = undefined) => {};
class Foo {
bar = undefined;
}
何时不使用
WuyCwB8qo4nmvu5MTb4w0VV0kpDrscHAbX1Y5ej/zqXUfE3VtVvDgbz3NFt160ZiYS5nZ7sGlSCqLa5HfwqssYU+ADGYPNw5CPtQupYSn/62AuxIkOYHKChV1uWw8EAjz57QIRmp+voJEu8Mgm0mIwa6iTcphFDIw2+YrtKAWplF7brRNJJMsihKEyClYgF7lKT9Zu7erIAXWnYlQESzBgnwfIwlRc1bNEssmet1CoY=
QnwhPm/cyTBJK+3dwgoJPe3E20un+2KP2CiFhQ+78TgFiUScrcmb3urZ1LYB2VeR
for (i = 0; i < 10; i++) {
var x = undefined;
console.log(x);
x = i;
}
NMMm+H5Sb4QiNOlt8nyUvchoy79NXarUv6f2MKzrE/0IjbcjHN2mIzZhrDBDE3Fhod4N5OjyN73b/mrbb7CxJqEUcVIAfuYvc+WP1HmGIlc=
var x;
for (i = 0; i < 10; i++) {
x = undefined;
console.log(x);
x = i;
}
2BMYD/zKGaRhcR91YPWEoLhmKq+wzdbmKAaBN/Ssj+C6O3A9tW2So5Q6jq3MQc0avWX0QWI5GmPLV5pB9KAaIA==
for (i = 0; i < 10; i++) {
var x;
console.log(x);
x = i;
}
SIao51gtTtVx1l8fz5TTKlat1RmMeo6+bhWko2D/yjY=
var x;
for (i = 0; i < 10; i++) {
console.log(x);
x = i;
}
V2npLmmdaySfO9/hT1vqTLwePGveoDv51AQEeztjI9xienisvdmRwt/du/m7SxeJcamrEfAb9ndnEtCrO8jBTtAxi63yOvrPY0aSCyezts9edpC8rwyejmB2itJDYIqaD8vTPK1j1JPYcrYpWDyGG6gLiBghxbu7+joJ4f7KT+03xP7McHGVZijhfZPlQMy/vooAN3yM8jZVDxNxmCGOvAe6o4zMWGBZIyTQscDOn+w=
jSdCQUKTYQDyelyHp9dKOsWyFwKlvgzOcjX68bZ1DiIh4abru5eY07kiV27K3Vgn38Nlp7987AlCqONyy5IoXrQR40uNZ/3oG9me7vbP95yC4YYuQF1GFQtfibfRWeo0
ZV3WTnsA6mjxcovKoGG8hTv7+Ha1z2USrMCyxcmIv+4lVx2DGCdLLsNdsGcWMAdYeCgVjjeMTeqp/1wGj6LeqAt8O/IxUN2Q4E1P9RHnOzw=
/*eslint no-undef-init: "error"*/
for (i = 0; i < 10; i++) {
var x = undefined; // eslint-disable-line no-undef-init
console.log(x);
x = i;
}