no-underscore-dangle

禁止标识符中的悬空下划线

就标识符的命名约定而言,悬空下划线可能是 JavaScript 中最极端的。悬空下划线是标识符开头或结尾的下划线,例如:

var _foo;

实际上,在 JavaScript 中使用悬空下划线来指示对象的 "private" 成员实际上由来已久(尽管 JavaScript 没有真正的私有成员,但这种约定是一种警告)。这始于 SpiderMonkey 添加非标准方法,例如 __defineGetter__()。带有下划线的目的是为了表明这种方法在某些方面是特殊的。从那时起,使用单个下划线前缀作为一种表示对象的 "private" 成员的方式变得流行起来。

您是否选择允许在标识符中使用悬空下划线纯粹是一种约定,对性能、可读性或复杂性没有影响。这纯粹是一种偏好。

规则详情

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

IgbHXOREhuUzqrkIHAUBELO1IBTM0/W5rjS4T6KeTRJddVuoa/qPZgBhw0wa2au1

/*eslint no-underscore-dangle: "error"*/

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

FphvHt5MK/G5o4JKMpo8wFZScNX7kGyQzE3xqEeYlFcVQkbgr+qufrIGgDR/9BRp

/*eslint no-underscore-dangle: "error"*/

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

选项

tcO8zSqCsgfoisRX4ASY5u7T9YxR5IVykd+e9kMOVkZzNZZBupg8S5OL9840zIKI

    cARTTmIo1/KkGLdH4j+7qf3ZwWJ38x2X9h2ONFYOD4dhUWsSxOvqDU3rXd2fOZtrEWDKM6W0sfpgsCWXsSa32Z2UNMOZ+Eo3sr/qLbWjuhk/svWYqhS1i4zchC9/+XLEWwYYFCdxAe8g0ybHkDaiip/7u5S/QxTWsKLQGqim95tfQTMIvI1/tz5cyWgBIO/rvxjyNzIehcC0wUpA7W2dz/r7YLUCwymjwZqLMI9T/8ExqgJtsxVj/70UJ1gBH6NrpHavvt2ic/5oirjpRh9vyyx1fo00VioATcpmC42gQeB3r960VuAUQH6PgSn708JQF64Xnmijq7ToJqg9GQ+7d33eKCSToRVWoIAiFtFRbShRHbXS2J1FZWT7ssIPkDuLmNFaKBD9viDwi3HhOQpwsCnFYkAZpghLcEB2oVLoqJn3EZStPjwcduta38bJtfYtwPGNYsN1m0To1SvGuxYlGv9pHxJpJaL696qNQM16c3jPzXQjzzETJOAL8uw2eUkQXZVoYZFvZfCWW4f2gj4f0j0HaLsN3gGiigliOx3tqFyG3pxvZzRu4M0e305QclplcEsFKDyc/oDAkj6IDnCcZbfN2gaFr0Rmf6EnrRf9ItpyT2e1upYXZdyY4atMCx3ZQoqwHm0krKhKSsz0T7QyO65m0hlTxEt63fPZ01V4NGFQQiZvPgbwaCoYV5+kFE8HEHOa/blwkWhF/z/GUzK29RnUwat5EqTsmX6W870G7SKspLDycNOXzQbEWCaWIhqHxDLm4bYiVv7arzeP9XH8cN6xT6Y2IrEiUqB7tDiynYFzDc24LB8sXrJ95JD6S1PZ6ksvbyO30s94Z4m1H2MKCpGfb0zQCS1+dHE8e+nWeogruOit+17CQplUYhn4o55jl4oHtQ0CBQwb1qFJh4b2/uG2B/f+DjBFggFNt1Yot8/GrFNaxKStnpTVXV6jpcI94rYWmkfs8RUt2kwU3NqL7CrnU8SrnwGIOD9vmNZk46RO87ZKTUB5vdXa1gyqFdkGn37Fr2ioFeaBhJunCCTu/T+LPm34oXEPnGKJQ967forwN5GBTb4h0XG2TORFhcNSJL2bwjJtBPKqFhWRSLYJvw==

allow

nQP82WYZJTVvhOZX4UTE6Eg0hfqimQ49J3Ipj6BxZU59GQMOCAl+geEp4RyRYiowgbrkcE31a0NPUYvCxy9eDKhud/2ESs8P86TPL4ANSVlh8LETcEZmIAWonnJT5I5m

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

var foo_;
foo._bar();

allowAfterThis

nQP82WYZJTVvhOZX4UTE6KFRAS7rRlCuQCMvsdIyG6d1bBau1MZBSWrI8Yx1GnGNDLCnSKezzNSqyZRDsHfNld0juzqd8UqdPfD64Ghs6h5i5AIfuLWNc609O4PW6bgj

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

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

allowAfterSuper

nQP82WYZJTVvhOZX4UTE6KFRAS7rRlCuQCMvsdIyG6cX9rjFJL4MQhNQG71ZVcW3MH+sXlJE8NrN8TEbiaSyx1cALLVifYYNAFDw8RoIDsXki+B8iQlpTyxkd3DkVu0F

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

var a = super.foo_;
super._bar();

allowAfterThisConstructor

nQP82WYZJTVvhOZX4UTE6KFRAS7rRlCuQCMvsdIyG6dve9y0CVLlfXRpYW8yS8pmhIEQDvTuVL3qPLPLbOCX8SIFtpiQpSQN39eHpDY9KP/3alKa8Q5mtZHl96PLMPu7xDRP0j/MCp8dmObxwXw3MA==

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

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

enforceInMethodNames

nQP82WYZJTVvhOZX4UTE6AML/b/qz5ktnPEYQHfVBIO5Yo7Hs2fcTlip8hm2BnomAXW0GtocyYiGpU2LiooAMi2ZisDmB+i6AciLjHgBaslMKohy5hzDVPg9NuEehR5w

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

class Foo {
  _bar() {}
}

class Foo {
  bar_() {}
}

const o = {
  _bar() {}
};

const o = {
  bar_() = {}
};

enforceInClassFields

nQP82WYZJTVvhOZX4UTE6AML/b/qz5ktnPEYQHfVBIPzVS6MZ8jxrE4tcyPcK/MfHpUdth/KCdbxYU//70euPI2DUIaZZNQds0DJQVOXvHwF+jDSwWSg/bNF+XhRhMOb

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

class Foo {
    _bar;
}

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

class Foo {
    bar_;
}

class Foo {
    #_bar;
}

class Foo {
    #bar_;
}

allowFunctionParams

nQP82WYZJTVvhOZX4UTE6EoeHCYcEDU6PGQMjGg9KPDkoBrdE7q1zdIQyXAzmsZZ+OuT467KyRM/+y8dTAAnl3rsRgn24ptorc5Moyp+0PQXMqcWiC+VwPVnCw8XORSC

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

function foo (_bar) {}
function foo (_bar = 0) {}
function foo (..._bar) {}

const foo = function onClick (_bar) {}
const foo = function onClick (_bar = 0) {}
const foo = function onClick (..._bar) {}

const foo = (_bar) => {};
const foo = (_bar = 0) => {};
const foo = (..._bar) => {};

何时不使用

fY1I6hkD90H0Y75TU0JpbAPe70GS+8qZmfd8EakUiy0Ls16pg3ozIT9N+rr37xsb/qzWYd8ecat88ipLYcvBAkishn/EWprU3F/xQ9LISOfsAfnG7Lq2wfGnFXCTdKl6