Index

no-underscore-dangle

不允许在标识符中使用悬挂下划线

❄️ Frozen

此规则目前已 冻结,不接受功能请求。

就标识符的命名约定而言,悬挂下划线可能是 JavaScript 中最具争议的。悬挂下划线是出现在标识符开头或结尾的下划线,例如:

🌐 As far as naming conventions for identifiers go, dangling underscores may be the most polarizing in JavaScript. Dangling underscores are underscores at either the beginning or end of an identifier, such as:

let _foo;

在 JavaScript 中,用悬挂下划线标记“私有”成员有着悠久的历史,这可以追溯到 SpiderMonkey 添加非标准方法如 __defineGetter__() 的时期。从那时起,使用单下划线前缀已成为最流行的约定,用于表示某个成员不是对象公共接口的一部分。

🌐 There is a long history of marking “private” members with dangling underscores in JavaScript, beginning with SpiderMonkey adding nonstandard methods such as __defineGetter__(). Since that time, using a single underscore prefix has become the most popular convention for indicating a member is not part of the public interface of an object.

建议使用 ECMAScript 2022 引入的正式私有类特性来封装私有数据和方法,而不是依赖命名约定。

🌐 It is recommended to use the formal private class features introduced in ECMAScript 2022 for encapsulating private data and methods rather than relying on naming conventions.

允许标识符中出现悬挂下划线纯粹是一种约定,对性能、可读性或复杂性没有影响。即使启用了此规则,它们也没有像私有类特性那样的封装优势。

🌐 Allowing dangling underscores in identifiers is purely a convention and has no effect on performance, readability, or complexity. They do not have the same encapsulation benefits as private class features, even with this rule enabled.

规则详情

🌐 Rule Details

此规则不允许在标识符中使用悬空下划线。

🌐 This rule disallows dangling underscores in identifiers.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-underscore-dangle: "error"*/

let foo_;
const __proto__ = {};
foo._bar();

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/*eslint no-underscore-dangle: "error"*/

const _ = require('underscore');
const obj = _.contains(items, item);
obj.__proto__ = {};
const file = __filename;
function foo(_bar) {};
const bar = { onClick(_bar) {} };
const baz = (_bar) => {};

选项

🌐 Options

此规则有一个对象选项:

🌐 This rule has an object option:

  • "allow" 允许指定的标识符有尾随下划线
  • "allowAfterThis": false(默认)不允许在 this 对象的成员中出现悬空下划线
  • "allowAfterSuper": false(默认)不允许在 super 对象的成员中出现悬空下划线
  • "allowAfterThisConstructor": false(默认)不允许在 this.constructor 对象的成员中出现悬空下划线
  • "enforceInMethodNames": false(默认)允许方法名中出现悬空下划线
  • "enforceInClassFields": false(默认)允许在 es2022 类字段名称中使用悬挂下划线
  • "allowInArrayDestructuring": true(默认)允许在通过数组解构赋值的变量名中使用悬空下划线
  • "allowInObjectDestructuring": true(默认)允许在通过对象解构赋值的变量名中使用悬空下划线
  • "allowFunctionParams": true(默认)允许函数参数名称中有悬挂下划线

allow

使用 { "allow": ["foo_", "_bar"] } 选项时,此规则的其他 正确 代码示例:

🌐 Examples of additional correct code for this rule with the { "allow": ["foo_", "_bar"] } option:

在线运行
/*eslint no-underscore-dangle: ["error", { "allow": ["foo_", "_bar"] }]*/

let foo_;
foo._bar();

allowAfterThis

使用 { "allowAfterThis": true } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { "allowAfterThis": true } option:

在线运行
/*eslint no-underscore-dangle: ["error", { "allowAfterThis": true }]*/

const a = this.foo_;
this._bar();

allowAfterSuper

使用 { "allowAfterSuper": true } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { "allowAfterSuper": true } option:

在线运行
/*eslint no-underscore-dangle: ["error", { "allowAfterSuper": true }]*/

class Foo extends Bar {
  doSomething() {
    const a = super.foo_;
    super._bar();
  }
}

allowAfterThisConstructor

使用 { "allowAfterThisConstructor": true } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { "allowAfterThisConstructor": true } option:

在线运行
/*eslint no-underscore-dangle: ["error", { "allowAfterThisConstructor": true }]*/

const a = this.constructor.foo_;
this.constructor._bar();

enforceInMethodNames

使用 { "enforceInMethodNames": true } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "enforceInMethodNames": true } option:

在线运行
/*eslint no-underscore-dangle: ["error", { "enforceInMethodNames": true }]*/

class Foo {
  _bar() {}
}

class Bar {
  bar_() {}
}

const o1 = {
  _bar() {}
};

const o2 = {
  bar_() {}
};

enforceInClassFields

使用 { "enforceInClassFields": true } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "enforceInClassFields": true } option:

在线运行
/*eslint no-underscore-dangle: ["error", { "enforceInClassFields": true }]*/

class Foo {
    _bar;
}

class Bar {
    _bar = () => {};
}

class Baz {
    bar_;
}

class Qux {
    #_bar;
}

class FooBar {
    #bar_;
}

allowInArrayDestructuring

使用 { "allowInArrayDestructuring": false } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "allowInArrayDestructuring": false } option:

在线运行
/*eslint no-underscore-dangle: ["error", { "allowInArrayDestructuring": false }]*/

const [_foo, _bar] = list;
const [foo_, ..._qux] = list;
const [foo, [bar, _baz]] = list;

allowInObjectDestructuring

使用 { "allowInObjectDestructuring": false } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "allowInObjectDestructuring": false } option:

在线运行
/*eslint no-underscore-dangle: ["error", { "allowInObjectDestructuring": false }]*/

const { foo, bar: _bar } = collection;
const { qux, xyz, _baz } = collection;

使用 { "allowInObjectDestructuring": false } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { "allowInObjectDestructuring": false } option:

在线运行
/*eslint no-underscore-dangle: ["error", { "allowInObjectDestructuring": false }]*/

const { foo, bar, _baz: { a, b } } = collection;
const { qux, xyz, _baz: baz } = collection;

allowFunctionParams

使用 { "allowFunctionParams": false } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "allowFunctionParams": false } option:

在线运行
/*eslint no-underscore-dangle: ["error", { "allowFunctionParams": false }]*/

function foo1 (_bar) {}
function foo2 (_bar = 0) {}
function foo3 (..._bar) {}

const foo4 = function onClick (_bar) {}
const foo5 = function onClick (_bar = 0) {}
const foo6 = function onClick (..._bar) {}

const foo7 = (_bar) => {};
const foo8 = (_bar = 0) => {};
const foo9 = (..._bar) => {};

何时不使用

🌐 When Not To Use It

如果你想在标识符中允许悬挂下划线,那么你可以安全地关闭此规则。

🌐 If you want to allow dangling underscores in identifiers, then you can safely turn this rule off.

版本

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

资源