init-declarations

要求或禁止在变量声明中进行初始化

在 JavaScript 中,变量可以在声明期间赋值,或者在之后的任何时候使用赋值语句。例如,在下面的代码中,foo 在声明时被初始化,而 bar 在后面被初始化。

¥In JavaScript, variables can be assigned during declaration, or at any point afterwards using an assignment statement. For example, in the following code, foo is initialized during declaration, while bar is initialized later.

var foo = 1;
var bar;

if (foo) {
    bar = 1;
} else {
    bar = 2;
}

规则详情

¥Rule Details

此规则旨在在声明期间强制或消除变量初始化。例如,在下面的代码中,foo 在声明时被初始化,而 bar 没有。

¥This rule is aimed at enforcing or eliminating variable initializations during declaration. For example, in the following code, foo is initialized during declaration, while bar is not.

var foo = 1;
var bar;

bar = 2;

该规则旨在为变量初始化和声明带来一致性。

¥This rule aims to bring consistency to variable initializations and declarations.

选项

¥Options

该规则有两个选项:

¥The rule takes two options:

  1. 一个字符串,必须是 "always"(默认值),以在声明时强制初始化,或 "never" 以在声明期间禁止初始化。此规则适用于 varletconst 变量,但对于 const 变量,忽略 "never",因为未分配的 const 会生成解析错误。

    ¥A string which must be either "always" (the default), to enforce initialization at declaration, or "never" to disallow initialization during declaration. This rule applies to var, let, and const variables, however "never" is ignored for const variables, as unassigned consts generate a parse error.

  2. 进一步控制此规则行为的对象。目前,唯一可用的参数是 ignoreForLoopInit,它指示在设置 "never" 时是否允许在 for 循环中进行声明时初始化,因为这是一个非常典型的用例。

    ¥An object that further controls the behavior of this rule. Currently, the only available parameter is ignoreForLoopInit, which indicates if initialization at declaration is allowed in for loops when "never" is set, since it is a very typical use case.

你可以按如下方式配置规则:

¥You can configure the rule as follows:

变量必须在声明时初始化(默认)

¥Variables must be initialized at declaration (default)

{
    "init-declarations": ["error", "always"],
}

变量不能在声明时初始化

¥Variables must not be initialized at declaration

{
    "init-declarations": ["error", "never"]
}

变量不得在声明时初始化,除非在允许的 for 循环中

¥Variables must not be initialized at declaration, except in for loops, where it is allowed

{
    "init-declarations": ["error", "never", { "ignoreForLoopInit": true }]
}

always

默认 "always" 选项的错误代码示例:

¥Examples of incorrect code for the default "always" option:

在线运行
/*eslint init-declarations: ["error", "always"]*/

function foo() {
    var bar;
    let baz;
}

默认 "always" 选项的正确代码示例:

¥Examples of correct code for the default "always" option:

在线运行
/*eslint init-declarations: ["error", "always"]*/

function foo() {
    var bar = 1;
    let baz = 2;
    const qux = 3;
}

never

"never" 选项的错误代码示例:

¥Examples of incorrect code for the "never" option:

在线运行
/*eslint init-declarations: ["error", "never"]*/

function foo() {
    var bar = 1;
    let baz = 2;

    for (var i = 0; i < 1; i++) {}
}

"never" 选项的正确代码示例:

¥Examples of correct code for the "never" option:

在线运行
/*eslint init-declarations: ["error", "never"]*/

function foo() {
    var bar;
    let baz;
    const buzz = 1;
}

"never" 选项忽略 const 变量初始化。

¥The "never" option ignores const variable initializations.

ignoreForLoopInit

"never", { "ignoreForLoopInit": true } 选项的正确代码示例:

¥Examples of correct code for the "never", { "ignoreForLoopInit": true } options:

在线运行
/*eslint init-declarations: ["error", "never", { "ignoreForLoopInit": true }]*/
for (var i = 0; i < 1; i++) {}

何时不使用

¥When Not To Use It

当你对变量的初始化方式漠不关心时。

¥When you are indifferent as to how your variables are initialized.

版本

此规则是在 ESLint v1.0.0-rc-1 中引入。

资源

ESLint 中文网
粤ICP备13048890号