File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -1417,6 +1417,38 @@ Other Style Guides
1417
1417
return name;
1418
1418
}
1419
1419
` ` `
1420
+ <a name=variables--no-chain-assignment""></a><a name="13.5"></a>
1421
+ - [13.5](#variables--no-chain-assignment) Don't chain variable assignments. eslint: [` one- var ` ](http://eslint.org/docs/rules/one-var.html)
1422
+
1423
+ > Why? Chaining variable assignments creates implicit global variables.
1424
+
1425
+ ` ` ` javascript
1426
+ // bad
1427
+ (function example () {
1428
+ // JavaScript interprets this as
1429
+ // let a = ( b = ( c = 1 ) );
1430
+ // The let keyword only applies to variable a; variables b and c become
1431
+ // global variables.
1432
+ let a = b = c = 1 ;
1433
+ }());
1434
+
1435
+ console .log (a); // undefined
1436
+ console .log (b); // 1
1437
+ console .log (c); // 1
1438
+
1439
+ // good
1440
+ (function example () {
1441
+ let a = 1 ;
1442
+ let b = a;
1443
+ let c = a;
1444
+ }());
1445
+
1446
+ console .log (a); // undefined
1447
+ console .log (b); // undefined
1448
+ console .log (c); // undefined
1449
+
1450
+ // the same applies for `const`
1451
+ ` ` `
1420
1452
1421
1453
**[⬆ back to top](#table-of-contents)**
1422
1454
You can’t perform that action at this time.
0 commit comments