1 # disallow dangling underscores in identifiers (no-underscore-dangle)
3 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:
9 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.
11 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.
15 This rule disallows dangling underscores in identifiers.
17 Examples of **incorrect** code for this rule:
20 /*eslint no-underscore-dangle: "error"*/
27 Examples of **correct** code for this rule:
30 /*eslint no-underscore-dangle: "error"*/
32 var _ = require('underscore');
33 var obj = _.contains(items, item);
35 var file = __filename;
36 function foo(_bar) {};
37 const foo = { onClick(_bar) {} };
38 const foo = (_bar) => {};
43 This rule has an object option:
45 - `"allow"` allows specified identifiers to have dangling underscores
46 - `"allowAfterThis": false` (default) disallows dangling underscores in members of the `this` object
47 - `"allowAfterSuper": false` (default) disallows dangling underscores in members of the `super` object
48 - `"allowAfterThisConstructor": false` (default) disallows dangling underscores in members of the `this.constructor` object
49 - `"enforceInMethodNames": false` (default) allows dangling underscores in method names
50 - `"allowFunctionParams": true` (default) allows dangling underscores in function parameter names
54 Examples of additional **correct** code for this rule with the `{ "allow": ["foo_", "_bar"] }` option:
57 /*eslint no-underscore-dangle: ["error", { "allow": ["foo_", "_bar"] }]*/
65 Examples of **correct** code for this rule with the `{ "allowAfterThis": true }` option:
68 /*eslint no-underscore-dangle: ["error", { "allowAfterThis": true }]*/
76 Examples of **correct** code for this rule with the `{ "allowAfterSuper": true }` option:
79 /*eslint no-underscore-dangle: ["error", { "allowAfterSuper": true }]*/
85 ### allowAfterThisConstructor
87 Examples of **correct** code for this rule with the `{ "allowAfterThisConstructor": true }` option:
90 /*eslint no-underscore-dangle: ["error", { "allowAfterThisConstructor": true }]*/
92 var a = this.constructor.foo_;
93 this.constructor._bar();
96 ### enforceInMethodNames
98 Examples of **incorrect** code for this rule with the `{ "enforceInMethodNames": true }` option:
101 /*eslint no-underscore-dangle: ["error", { "enforceInMethodNames": true }]*/
120 ### allowFunctionParams
122 Examples of **incorrect** code for this rule with the `{ "allowFunctionParams": false }` option:
125 /*eslint no-underscore-dangle: ["error", { "allowFunctionParams": false }]*/
127 function foo (_bar) {}
128 function foo (_bar = 0) {}
129 function foo (..._bar) {}
131 const foo = function onClick (_bar) {}
132 const foo = function onClick (_bar = 0) {}
133 const foo = function onClick (..._bar) {}
135 const foo = (_bar) => {};
136 const foo = (_bar = 0) => {};
137 const foo = (..._bar) => {};
140 ## When Not To Use It
142 If you want to allow dangling underscores in identifiers, then you can safely turn this rule off.