From b1260164108fbdba88946f650f7a0ab1313bdf10 Mon Sep 17 00:00:00 2001 From: Aniruddh Agarwal Date: Wed, 17 Aug 2016 13:20:08 +0800 Subject: [PATCH] Add section on variable assignment chaining --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 851b46e1..b3975e2e 100644 --- a/README.md +++ b/README.md @@ -1417,6 +1417,38 @@ Other Style Guides return name; } ``` + + - [13.5](#variables--no-chain-assignment) Don't chain variable assignments. eslint: [`one-var`](http://eslint.org/docs/rules/one-var.html) + + > Why? Chaining variable assignments creates implicit global variables. + + ```javascript + // bad + (function example() { + // JavaScript interprets this as + // let a = ( b = ( c = 1 ) ); + // The let keyword only applies to variable a; variables b and c become + // global variables. + let a = b = c = 1; + }()); + + console.log(a); // undefined + console.log(b); // 1 + console.log(c); // 1 + + // good + (function example() { + let a = 1; + let b = a; + let c = a; + }()); + + console.log(a); // undefined + console.log(b); // undefined + console.log(c); // undefined + + // the same applies for `const` + ``` **[⬆ back to top](#table-of-contents)**