dot-notation

尽可能使用点符号

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行选项自动修复

在 JavaScript 中,可以使用点表示法 (foo.bar) 或方括号表示法 (foo["bar"]) 访问属性。但是,点表示法通常更受欢迎,因为它更易于阅读、不那么冗长,并且可以更好地与激进的 JavaScript 最小化程序配合使用。

¥In JavaScript, one can access properties using the dot notation (foo.bar) or square-bracket notation (foo["bar"]). However, the dot notation is often preferred because it is easier to read, less verbose, and works better with aggressive JavaScript minimizers.

foo["bar"];

规则详情

¥Rule Details

此规则旨在通过鼓励尽可能使用点符号样式来保持代码一致性并提高代码可读性。因此,它会在遇到不必要的方括号符号使用时触发警告。

¥This rule is aimed at maintaining code consistency and improving code readability by encouraging use of the dot notation style whenever possible. As such, it will warn when it encounters an unnecessary use of square-bracket notation.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint dot-notation: "error"*/

var x = foo["bar"];

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint dot-notation: "error"*/

var x = foo.bar;

var x = foo[bar];    // Property name is a variable, square-bracket notation required

选项

¥Options

此规则接受单个选项参数:

¥This rule accepts a single options argument:

  • allowKeywords 选项设置为 false(默认为 true)以遵循 ECMAScript 版本 3 兼容样式,避免保留字属性的点表示法。

    ¥Set the allowKeywords option to false (default is true) to follow ECMAScript version 3 compatible style, avoiding dot notation for reserved word properties.

  • allowPattern 选项设置为正则表达式字符串,以允许对匹配模式的属性名称使用括号表示法(默认情况下,不测试任何模式)。

    ¥Set the allowPattern option to a regular expression string to allow bracket notation for property names that match a pattern (by default, no pattern is tested).

allowKeywords

{ "allowKeywords": false } 选项的正确代码示例:

¥Examples of correct code for the { "allowKeywords": false } option:

在线运行
/*eslint dot-notation: ["error", { "allowKeywords": false }]*/

var foo = { "class": "CS 101" }
var x = foo["class"]; // Property name is a reserved word, square-bracket notation required

{ "allowKeywords": false } 选项的附加正确代码示例:

¥Examples of additional correct code for the { "allowKeywords": false } option:

在线运行
/*eslint dot-notation: ["error", { "allowKeywords": false }]*/

class C {
    #in;
    foo() {
        this.#in; // Dot notation is required for private identifiers
    }
}

allowPattern

例如,在准备要发送到外部 API 的数据时,通常需要使用包含下划线的属性名称。如果 camelcase 规则生效,则不允许使用这些 蛇案 属性。通过向 dot-notation 规则提供 allowPattern,可以使用括号表示法访问这些蛇形案例属性。

¥For example, when preparing data to be sent to an external API, it is often required to use property names that include underscores. If the camelcase rule is in effect, these snake case properties would not be allowed. By providing an allowPattern to the dot-notation rule, these snake case properties can be accessed with bracket notation.

示例 { "allowPattern": "^[a-z]+(_[a-z]+)+$" }(查找蛇形命名属性的模式)选项的错误代码示例:

¥Examples of incorrect code for the sample { "allowPattern": "^[a-z]+(_[a-z]+)+$" } (pattern to find snake case named properties) option:

在线运行
/*eslint dot-notation: ["error", { "allowPattern": "^[a-z]+(_[a-z]+)+$" }]*/

var data = {};
data["fooBar"] = 42;

示例 { "allowPattern": "^[a-z]+(_[a-z]+)+$" }(查找蛇形命名属性的模式)选项的正确代码示例:

¥Examples of correct code for the sample { "allowPattern": "^[a-z]+(_[a-z]+)+$" } (pattern to find snake case named properties) option:

在线运行
/*eslint dot-notation: ["error", { "allowPattern": "^[a-z]+(_[a-z]+)+$" }]*/

var data = {};
data["foo_bar"] = 42;

版本

此规则是在 ESLint v0.0.7 中引入。

资源

ESLint 中文网
粤ICP备13048890号