- Two space indention
- No trailing whitespace
- End-of-file newlines in all text files
- Prefer the latest ECMAScript syntax available on the target platform(s)
- Try to keep lines less than 80 characters wide
- Use files for additional scope rather than closures
- Prefer
'
over"
- avoid escaping quotations
Except when necessary, such as in for(;;){}
.
Do not argue about semicolons or the lack thereof. There are better, more important fights to fight.
function () {
// =^.^=
}
var one = 1
var two = 2
function fn() {}
Exception: Conditionals - var fn = a ? function () {} : function () {}
function (a, b) {}
function fn(a, b) {}
if () {} else {}
switch () {}
for (a; b >= c; c + d / e) {}
// ++ -- ~ ! are exceptions
a++
--b
!!~array.indexOf('c')
function fn(a, b) {}
if (a, b, c) {}
for (a; b; c) {}
If the attributes are likely to change, define each element on a separate line.
Add trailing commas to multiline definitions.
var thing = {
not_changing: ['c', 'a', 't'],
might_change: [
'bitcoin',
],
a: 'z',
b: 'y',
}
parallel([fn1, fn2], callback)
function fn1() {}
function fn2() {}
var req = http.get(function (res) {
// still makes sense
})
If necessary, prepend with a semicolon.
;[].forEach()
;(function () {})()
Code should philosophically be ordered:
- From high level to lower level
- From public to private
Code in a single file should generally be structured in the following order:
- Global imports -
require('lib')
- Relative imports -
require('./src')
- Variables -
var a = 1
- Exports -
module.exports = Main
- Main -
function Main() {}
- Prototype Properties -
Main.prototype.something = true
- Utilities -
function add(a, b) {return a + b}