2 title: no-underscore-dangle
8 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:
14 There is actually a long history of using dangling underscores to indicate "private" members of objects in JavaScript (though JavaScript doesn't have truly private members, this convention served as a warning). This began with SpiderMonkey adding nonstandard methods such as `__defineGetter__()`. The intent with the underscores was to make it obvious that this method was special in some way. Since that time, using a single underscore prefix has become popular as a way to indicate "private" members of objects.
16 Whether or not you choose to allow dangling underscores in identifiers is purely a convention and has no effect on performance, readability, or complexity. It's purely a preference.
20 This rule disallows dangling underscores in identifiers.
22 Examples of **incorrect** code for this rule:
27 /*eslint no-underscore-dangle: "error"*/
36 Examples of **correct** code for this rule:
41 /*eslint no-underscore-dangle: "error"*/
43 var _ = require('underscore');
44 var obj = _.contains(items, item);
46 var file = __filename;
47 function foo(_bar) {};
48 const foo = { onClick(_bar) {} };
49 const foo = (_bar) => {};
56 This rule has an object option:
58 * `"allow"` allows specified identifiers to have dangling underscores
59 * `"allowAfterThis": false` (default) disallows dangling underscores in members of the `this` object
60 * `"allowAfterSuper": false` (default) disallows dangling underscores in members of the `super` object
61 * `"allowAfterThisConstructor": false` (default) disallows dangling underscores in members of the `this.constructor` object
62 * `"enforceInMethodNames": false` (default) allows dangling underscores in method names
63 * `"enforceInClassFields": false` (default) allows dangling underscores in es2022 class fields names
64 * `"allowFunctionParams": true` (default) allows dangling underscores in function parameter names
68 Examples of additional **correct** code for this rule with the `{ "allow": ["foo_", "_bar"] }` option:
73 /*eslint no-underscore-dangle: ["error", { "allow": ["foo_", "_bar"] }]*/
83 Examples of **correct** code for this rule with the `{ "allowAfterThis": true }` option:
88 /*eslint no-underscore-dangle: ["error", { "allowAfterThis": true }]*/
98 Examples of **correct** code for this rule with the `{ "allowAfterSuper": true }` option:
103 /*eslint no-underscore-dangle: ["error", { "allowAfterSuper": true }]*/
111 ### allowAfterThisConstructor
113 Examples of **correct** code for this rule with the `{ "allowAfterThisConstructor": true }` option:
118 /*eslint no-underscore-dangle: ["error", { "allowAfterThisConstructor": true }]*/
120 var a = this.constructor.foo_;
121 this.constructor._bar();
126 ### enforceInMethodNames
128 Examples of **incorrect** code for this rule with the `{ "enforceInMethodNames": true }` option:
133 /*eslint no-underscore-dangle: ["error", { "enforceInMethodNames": true }]*/
154 ### enforceInClassFields
156 Examples of **incorrect** code for this rule with the `{ "enforceInClassFields": true }` option:
161 /*eslint no-underscore-dangle: ["error", { "enforceInClassFields": true }]*/
186 ### allowFunctionParams
188 Examples of **incorrect** code for this rule with the `{ "allowFunctionParams": false }` option:
193 /*eslint no-underscore-dangle: ["error", { "allowFunctionParams": false }]*/
195 function foo (_bar) {}
196 function foo (_bar = 0) {}
197 function foo (..._bar) {}
199 const foo = function onClick (_bar) {}
200 const foo = function onClick (_bar = 0) {}
201 const foo = function onClick (..._bar) {}
203 const foo = (_bar) => {};
204 const foo = (_bar = 0) => {};
205 const foo = (..._bar) => {};
210 ## When Not To Use It
212 If you want to allow dangling underscores in identifiers, then you can safely turn this rule off.