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+7d33eKCSTo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G3iTWPeKzkJEPjnqH7zsC8qPI=
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_;
}
allowInArrayDestructuring
nQP82WYZJTVvhOZX4UTE6GG4a+DRgpzoWmZWWBrzfbHb3lMcWr6s62smRIy8kZb2bxI8WdIVMJ84Bqe3livPUmmsK2aEH1RHaOcqJAK5IlXoHD6ERcraJRidzxvjmzb5qOyKgBXmKVORfWUKVAnu1w==
/*eslint no-underscore-dangle: ["error", { "allowInArrayDestructuring": false }]*/
const [_foo, _bar] = list;
const [foo_, ..._bar] = list;
const [foo, [bar, _baz]] = list;
allowInObjectDestructuring
nQP82WYZJTVvhOZX4UTE6GG4a+DRgpzoWmZWWBrzfbHjw3ixVNuNP7lCwcDicTB8veLHFN+rQYgLfQ5Hdy0NWEMiaeV4rDhtxeOWGIYfi3cBzWz7e1EtFuJgMxUNKFm/DkPwt7EEVJShJZHaYsqVqA==
/*eslint no-underscore-dangle: ["error", { "allowInObjectDestructuring": false }]*/
const { foo, bar: _bar } = collection;
const { foo, bar, _baz } = collection;
nQP82WYZJTVvhOZX4UTE6GG4a+DRgpzoWmZWWBrzfbHjw3ixVNuNP7lCwcDicTB8veLHFN+rQYgLfQ5Hdy0NWEaiQ+tQoasZkFFP/6pltlc5wf1wm8PQ56t8vJIFoQzKVez0G3stGK1Lbt8mebKTDg==
/*eslint no-underscore-dangle: ["error", { "allowInObjectDestructuring": false }]*/
const { foo, bar, _baz: { a, b } } = collection;
const { foo, bar, _baz: baz } = collection;
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