Skip to content

Commit f0781a8

Browse files
committed
Merge pull request airbnb#220 from justjake/master
Switch to one-variable-per-var from many-variables-per-var
2 parents bae9bc6 + 3c68fc4 commit f0781a8

File tree

1 file changed

+49
-35
lines changed

1 file changed

+49
-35
lines changed

README.md

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
+ `undefined`
5151

5252
```javascript
53-
var foo = 1,
54-
bar = foo;
53+
var foo = 1;
54+
var bar = foo;
5555

5656
bar = 9;
5757

@@ -64,8 +64,8 @@
6464
+ `function`
6565

6666
```javascript
67-
var foo = [1, 2],
68-
bar = foo;
67+
var foo = [1, 2];
68+
var bar = foo;
6969
7070
bar[0] = 9;
7171
@@ -151,9 +151,9 @@
151151
- When you need to copy an array use Array#slice. [jsPerf](http://jsperf.com/converting-arguments-to-an-array/7)
152152
153153
```javascript
154-
var len = items.length,
155-
itemsCopy = [],
156-
i;
154+
var len = items.length;
155+
var itemsCopy = [];
156+
var i;
157157
158158
// bad
159159
for (i = 0; i < len; i++) {
@@ -216,10 +216,10 @@
216216
- When programmatically building up a string, use Array#join instead of string concatenation. Mostly for IE: [jsPerf](http://jsperf.com/string-vs-array-concat/2).
217217
218218
```javascript
219-
var items,
220-
messages,
221-
length,
222-
i;
219+
var items;
220+
var messages;
221+
var length;
222+
var i;
223223
224224
messages = [{
225225
state: 'success',
@@ -366,18 +366,27 @@
366366
var superPower = new SuperPower();
367367
```
368368
369-
- Use one `var` declaration for multiple variables and declare each variable on a newline.
369+
- Use one `var` declaration per variable.
370+
It's easier to add new variable declarations this way, and you never have
371+
to worry about swapping out a `;` for a `,` or introducing punctuation-only
372+
diffs.
370373
371374
```javascript
372375
// bad
373-
var items = getItems();
374-
var goSportsTeam = true;
375-
var dragonball = 'z';
376-
377-
// good
378376
var items = getItems(),
379377
goSportsTeam = true,
380378
dragonball = 'z';
379+
380+
// bad
381+
// (compare to above, and try to spot the mistake)
382+
var items = getItems(),
383+
goSportsTeam = true;
384+
dragonball = 'z';
385+
386+
// good
387+
var items = getItems();
388+
var goSportsTeam = true;
389+
var dragonball = 'z';
381390
```
382391
383392
- Declare unassigned variables last. This is helpful when later on you might need to assign a variable depending on one of the previous assigned variables.
@@ -389,17 +398,18 @@
389398
goSportsTeam = true;
390399

391400
// bad
392-
var i, items = getItems(),
393-
dragonball,
394-
goSportsTeam = true,
395-
len;
401+
var i;
402+
var items = getItems();
403+
var dragonball;
404+
var goSportsTeam = true;
405+
var len;
396406

397407
// good
398-
var items = getItems(),
399-
goSportsTeam = true,
400-
dragonball,
401-
length,
402-
i;
408+
var items = getItems();
409+
var goSportsTeam = true;
410+
var dragonball;
411+
var length;
412+
var i;
403413
```
404414
405415
- Assign variables at the top of their scope. This helps avoid issues with variable declaration and assignment hoisting related issues.
@@ -639,8 +649,8 @@
639649
// make() returns a new element
640650
// based on the passed in tag name
641651
//
642-
// @param <String> tag
643-
// @return <Element> element
652+
// @param {String} tag
653+
// @return {Element} element
644654
function make(tag) {
645655

646656
// ...stuff...
@@ -653,8 +663,8 @@
653663
* make() returns a new element
654664
* based on the passed in tag name
655665
*
656-
* @param <String> tag
657-
* @return <Element> element
666+
* @param {String} tag
667+
* @return {Element} element
658668
*/
659669
function make(tag) {
660670

@@ -843,14 +853,18 @@
843853

844854
```javascript
845855
// bad
846-
var once
856+
var story = [
857+
once
847858
, upon
848-
, aTime;
859+
, aTime
860+
];
849861
850862
// good
851-
var once,
852-
upon,
853-
aTime;
863+
var story = [
864+
once,
865+
upon,
866+
aTime
867+
];
854868
855869
// bad
856870
var hero = {

0 commit comments

Comments
 (0)