no-underscore-dangle

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

就标识符的命名约定而言,悬空下划线可能是 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:

var _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"*/

var foo_;
var __proto__ = {};
foo._bar();

此规则的正确代码示例:

¥Examples of correct code for this rule:

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

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

选项

¥Options

此规则有一个对象选项:

¥This rule has an object option:

  • "allow" 允许指定标识符具有悬空下划线

    ¥"allow" allows specified identifiers to have dangling underscores

  • "allowAfterThis": false(默认)不允许 this 对象的成员中存在悬空下划线

    ¥"allowAfterThis": false (default) disallows dangling underscores in members of the this object

  • "allowAfterSuper": false(默认)不允许 super 对象的成员中存在悬空下划线

    ¥"allowAfterSuper": false (default) disallows dangling underscores in members of the super object

  • "allowAfterThisConstructor": false(默认)不允许 this.constructor 对象的成员中存在悬空下划线

    ¥"allowAfterThisConstructor": false (default) disallows dangling underscores in members of the this.constructor object

  • "enforceInMethodNames": false(默认)允许方法名称中包含悬空下划线

    ¥"enforceInMethodNames": false (default) allows dangling underscores in method names

  • "enforceInClassFields": false(默认)允许在 es2022 类字段名称中使用悬空下划线

    ¥"enforceInClassFields": false (default) allows dangling underscores in es2022 class fields names

  • "allowInArrayDestructuring": true(默认)允许在数组解构分配的变量名中使用悬空下划线

    ¥"allowInArrayDestructuring": true (default) allows dangling underscores in variable names assigned by array destructuring

  • "allowInObjectDestructuring": true(默认)允许在对象解构分配的变量名中使用悬空下划线

    ¥"allowInObjectDestructuring": true (default) allows dangling underscores in variable names assigned by object destructuring

  • "allowFunctionParams": true(默认)允许函数参数名称中包含悬空下划线

    ¥"allowFunctionParams": true (default) allows dangling underscores in function parameter names

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"] }]*/

var 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 }]*/

var 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() {
    var 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 }]*/

var 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 中引入。

资源