dot-notation

尽可能强制使用点表示法

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

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

foo["bar"];

规则详情

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

此规则的错误代码示例:

/*eslint dot-notation: "error"*/

var x = foo["bar"];

f/g6SKVZdhoBiPVtnee0m+uPEv1T4kMgIaFUpxdtFwzAZHGBqJ5dJxRHxHHP/cP8

/*eslint dot-notation: "error"*/

var x = foo.bar;

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

选项

s8U6fkMHLlw9xwLE9wEu7PScyGhNfB626MrUJUAFZrP6KHUUr2kWhPM8RiL6id6e

    F79dRjAxD+jeiTD20mF4ypCWseXk8qKtYvTExcX82sPt9LcJJsdnDcR9kSeW79FE4Hl11q/iprL+858ZS4QvBjC5unVg2tDoDtItOEUaYVzjHna5WazjyAcdHw8ZeYKiM2lvB50/oQuuILJG4nqxtsNjt1eDzkrcRWapBVjvHGPV8Foi1q7psqbomeogUvIU4muyKncXiX54OgCKBC8ZIorxzpTnlmz1silxKIG/qvL+oszPznwLcur6pGgvCVPS0CWGbRgJN0cYWlICWxJjEaFm6Iph5hnpKpSceQUXfYx0yL0zHMWpkiUrwfCFzTw7hutYKecfQ/d8Id+6Q1FX/FY8mO5bSkRrDmKZcreBB/KHqOcaco6OFMpdhhTvkxoJ7H3cUDsoXPqXyeYp4D4eO++HXBQhztrRc4ErolejfD3Ak+dVJNduvNzxkrYEA0+mUoTxSONYJ253HD2aCCEHIIaclbW3aHa/MpIxA7+E3eFlExFNIi6mIqGHeBSZJR/z

allowKeywords

15ZC+s45LrleRP3SX+RHVdwH94i1nwzf+wCwhaew7ZAJb4YJQAUe2bg2FEmOdwi75EjkDnmZtyj3X+6vfodYTG0F7esAp5/teaZPZ6HWV0Q=

/*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

15ZC+s45LrleRP3SX+RHVdwH94i1nwzf+wCwhaew7ZAJb4YJQAUe2bg2FEmOdwi7l/R3QlTiU2I5/vDRwoSDvM6pOEEewquYnQDFEQ6YLds=

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

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

allowPattern

Rg6uhPzt16YIGy1MpvfK7tSCeXAQdRuenXoTQwsaIMHxCOhRLgStZkpmrAIKyMF/KJwtTU+xST0/nIpvHDRNy31gL4Wogv11ZTu/lqh18jQjjQwgTEqxWvN+k5ajrpfPAr6Ft39uAljMOv7tpwIb8PLHRvzRU0M3R7WH6FDsolJCTKNyuvF1YLIrkxUjKVn1kO5Sqbn7dIassuVm7m0DJDUHez/pUYJEwnPJOq5Cc22yX1ReTfmy5Nj1rQWUqz+gNjO0tUq28DwNrMKnPRTScCYqsr1jF+I0vtl6Me+d86NQrPdfg5+idSZrfrvjlfMXR6MuxG35WCJug54ofM8eYP0ojKPGLQ84ph1t8ksoqZkBliIl1T/E58Ow+0b/gMG9zSwKhJ7OPEaQ0ECayjiHwCwS3Njj7Y1lV8Gyv+1RZ+gm7kHTYcYPtp5O5lZXO7va6sySbhnXOtV7/2CnSpjfmoauXjL2l++WV8O+HvVIxqK6DWMnmmX7RkDU+ObfcRai

ouOj74qXQ1hTxOgZfmw6fRLGLRidTadu8f7iM/7zBXTngL2bfXaiFVvI+zMeuOPzxJz/smccM/BXzmWIA3tBmK+B/WvjN1m7v1tQUsnKUBaFyVbs9HdmSxxhM+mqOf6q

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

var data = {};
data.foo_bar = 42;

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

var data = {};
data["foo_bar"] = 42; // no warning