Merge pull request #220 from justjake/master

Switch to one-variable-per-var from many-variables-per-var
This commit is contained in:
Harrison Shoff
2014-12-03 17:54:30 -08:00

View File

@@ -50,8 +50,8 @@
+ `undefined`
```javascript
var foo = 1,
bar = foo;
var foo = 1;
var bar = foo;
bar = 9;
@@ -64,8 +64,8 @@
+ `function`
```javascript
var foo = [1, 2],
bar = foo;
var foo = [1, 2];
var bar = foo;
bar[0] = 9;
@@ -151,9 +151,9 @@
- When you need to copy an array use Array#slice. [jsPerf](http://jsperf.com/converting-arguments-to-an-array/7)
```javascript
var len = items.length,
itemsCopy = [],
i;
var len = items.length;
var itemsCopy = [];
var i;
// bad
for (i = 0; i < len; i++) {
@@ -216,10 +216,10 @@
- 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).
```javascript
var items,
messages,
length,
i;
var items;
var messages;
var length;
var i;
messages = [{
state: 'success',
@@ -366,18 +366,27 @@
var superPower = new SuperPower();
```
- Use one `var` declaration for multiple variables and declare each variable on a newline.
- Use one `var` declaration per variable.
It's easier to add new variable declarations this way, and you never have
to worry about swapping out a `;` for a `,` or introducing punctuation-only
diffs.
```javascript
// bad
var items = getItems();
var goSportsTeam = true;
var dragonball = 'z';
// good
var items = getItems(),
goSportsTeam = true,
dragonball = 'z';
// bad
// (compare to above, and try to spot the mistake)
var items = getItems(),
goSportsTeam = true;
dragonball = 'z';
// good
var items = getItems();
var goSportsTeam = true;
var dragonball = 'z';
```
- 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 @@
goSportsTeam = true;
// bad
var i, items = getItems(),
dragonball,
goSportsTeam = true,
len;
var i;
var items = getItems();
var dragonball;
var goSportsTeam = true;
var len;
// good
var items = getItems(),
goSportsTeam = true,
dragonball,
length,
i;
var items = getItems();
var goSportsTeam = true;
var dragonball;
var length;
var i;
```
- Assign variables at the top of their scope. This helps avoid issues with variable declaration and assignment hoisting related issues.
@@ -639,8 +649,8 @@
// make() returns a new element
// based on the passed in tag name
//
// @param <String> tag
// @return <Element> element
// @param {String} tag
// @return {Element} element
function make(tag) {
// ...stuff...
@@ -653,8 +663,8 @@
* make() returns a new element
* based on the passed in tag name
*
* @param <String> tag
* @return <Element> element
* @param {String} tag
* @return {Element} element
*/
function make(tag) {
@@ -843,14 +853,18 @@
```javascript
// bad
var once
var story = [
once
, upon
, aTime;
, aTime
];
// good
var once,
upon,
aTime;
var story = [
once,
upon,
aTime
];
// bad
var hero = {