no-undef-init
不允许将变量初始化为 undefined
在 JavaScript 中,声明但未初始化为任何值的变量会自动获得 undefined 的值。例如:
🌐 In JavaScript, a variable that is declared and not initialized to any value automatically gets the value of undefined. For example:
var foo;
console.log(foo === undefined); // true
因此没有必要将变量初始化为 undefined,例如:
🌐 It’s therefore unnecessary to initialize a variable to undefined, such as:
var foo = undefined;
通常认为,避免将变量初始化为 undefined 是一种最佳实践。
🌐 It’s considered a best practice to avoid initializing variables to undefined.
规则详情
🌐 Rule Details
此规则旨在消除初始化为 undefined 的 var 和 let 变量声明。
🌐 This rule aims to eliminate var and let variable declarations that initialize to undefined.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint no-undef-init: "error"*/
var foo = undefined;
let bar = undefined;
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint no-undef-init: "error"*/
var foo;
let bar;
请注意,此规则不会检查 const 声明、using 声明、await using 声明、解构模式、函数参数和类字段。
🌐 Please note that this rule does not check const declarations, using declarations, await using declarations, destructuring patterns, function parameters, and class fields.
此规则的额外正确代码示例:
🌐 Examples of additional correct code for this rule:
/*eslint no-undef-init: "error"*/
const foo = undefined;
using foo1 = undefined;
await using foo2 = undefined;
let { bar = undefined } = baz;
[quux = undefined] = quuux;
(foo = undefined) => {};
class Foo {
bar = undefined;
}
选项
🌐 Options
此规则没有选项。
🌐 This rule has no options.
何时不使用
🌐 When Not To Use It
有些情况下,将其初始化为 undefined 的行为与省略初始化不同。
🌐 There are situations where initializing to undefined behaves differently than omitting the initialization.
其中一种情况是当 var 声明出现在循环内部。例如:
🌐 One such case is when a var declaration occurs inside of a loop. For example:
此规则的错误代码示例:
🌐 Example of incorrect code for this rule:
/*eslint no-undef-init: "error"*/
for (i = 0; i < 10; i++) {
var x = undefined;
console.log(x);
x = i;
}
在这种情况下,var x 被提升出了循环,有效地创建了:
🌐 In this case, the var x is hoisted out of the loop, effectively creating:
var x;
for (i = 0; i < 10; i++) {
x = undefined;
console.log(x);
x = i;
}
如果要删除初始化,则循环的行为会发生变化:
🌐 If you were to remove the initialization, then the behavior of the loop changes:
for (i = 0; i < 10; i++) {
var x;
console.log(x);
x = i;
}
这段代码相当于:
🌐 This code is equivalent to:
var x;
for (i = 0; i < 10; i++) {
console.log(x);
x = i;
}
这会产生与在循环中定义 var x = undefined 不同的结果,因为 x 不再在每次循环时重置为 undefined。
🌐 This produces a different outcome than defining var x = undefined in the loop, as x is no longer reset to undefined each time through the loop.
如果你在循环内使用这样的初始化,那么你应该禁用此规则。
🌐 If you’re using such an initialization inside of a loop, then you should disable this rule.
此规则的正确代码示例,因为它在特定行上被禁用:
🌐 Example of correct code for this rule, because it is disabled on a specific line:
/*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;
}
另一个此类情况是当使用 var 重新声明变量时。例如:
🌐 Another such case is when a variable is redeclared using var. For example:
function foo() {
var x = 1;
console.log(x); // output: 1
var x;
console.log(x); // output: 1
var x = undefined;
console.log(x); // output: undefined
}
foo();
在这种情况下,你可以通过将其更改为赋值(x = undefined;)来避免重新声明,或者在特定行使用 eslint 禁用注释。
🌐 In this case, you can avoid redeclaration by changing it to an assignment (x = undefined;), or use an eslint disable comment on a specific line.
相关规则
版本
此规则是在 ESLint v0.0.6 中引入。