Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 78 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -624,12 +624,47 @@
}
```

- Put `else` and `catch` on the same line with closing brace.

```javascript
// bad
if (test) {
return true;
}
else {
return false;
}

// good
if (test) {
return 1;
} else {
return -1;
}

// bad
try {
dragonsBeHere();
}
catch (e) {
log.error('very bad: ' + e);
}

// good
try {
dragonsBeHere();
} catch (e) {
log.error('very bad: ' + e);
}
```

**[⬆ back to top](#table-of-contents)**


## Comments

- Use `/** ... */` for multiline comments. Include a description, specify types and values for all parameters and return values.
- Use [JSDoc](http://usejsdoc.org)

```javascript
// bad
Expand All @@ -650,8 +685,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) {

Expand Down Expand Up @@ -691,21 +726,7 @@
}
```

- Prefixing your comments with `FIXME` or `TODO` helps other developers quickly understand if you're pointing out a problem that needs to be revisited, or if you're suggesting a solution to the problem that needs to be implemented. These are different than regular comments because they are actionable. The actions are `FIXME -- need to figure this out` or `TODO -- need to implement`.

- Use `// FIXME:` to annotate problems

```javascript
function Calculator() {

// FIXME: shouldn't use a global here
total = 0;

return this;
}
```

- Use `// TODO:` to annotate solutions to problems
- Use `@todo` to annotate todos

```javascript
function Calculator() {
Expand All @@ -722,7 +743,7 @@

## Whitespace

- Use soft tabs set to 2 spaces
- Use soft tabs set to 4 spaces

```javascript
// bad
Expand All @@ -737,7 +758,7 @@

// good
function() {
∙∙var name;
∙∙∙∙var name;
}
```

Expand Down Expand Up @@ -766,6 +787,20 @@
breed: 'Bernese Mountain Dog'
});
```

- Place 1 space after keywords (such as `if`, `for` and `function`).

```javascript
// bad
var foo = function() {
alert('hello');
};

// good
var foo = function () {
alert('hello');
};
```

- Set off operators with spaces.

Expand Down Expand Up @@ -1061,7 +1096,7 @@
this._firstName = 'Panda';
```

- When saving a reference to `this` use `_this`.
- When saving a reference to `this` use `me`.

```javascript
// bad
Expand All @@ -1074,26 +1109,44 @@

// bad
function() {
var that = this;
var _this = this;
return function() {
console.log(that);
console.log(_this);
};
}

// good
function() {
var _this = this;
var me = this;
return function() {
console.log(_this);
console.log(me);
};
}
```

- When implementing the JavaScript module pattern use `that` for referencing
the "public" interface.

```javascript

var mymodule = (function mymodule() {
var that = {},
privateCounter = 0;

that.increment = function increment() {
privateCounter += 1;
return privateCounter;
};

return that;
})();
```

- Name your functions. This is helpful for stack traces.

```javascript
// bad
var log = function(msg) {
var log = function (msg) {
console.log(msg);
};

Expand Down