一棵树

路漫漫其修远兮 吾将上下而求索。

导航

Node.js Style Guide 编码规范

1:用单引号
正确:var foo = 'bar'; 错误:var foo = “bar”;
2:Your opening braces go on the same line as the statement.
正确:
if (true) {
console.log('winning');
}
错误:
if (true)
{
console.log('losing');
}
3:Variable declarations 要单独声明
正确:
var keys = ['foo', 'bar'];
var values = [23, 42];

var object = {};
while (items.length) {
var key = keys.pop();
object[key] = values.pop();
}
错误:
var keys = ['foo', 'bar'],
values = [23, 42],
object = {},
key;

while (items.length) {
key = keys.pop();
object[key] = values.pop();
}
4:Variable and property names
正确:var adminUser = db.query('SELECT * FROM users ...');
错误:var admin_user = db.query('SELECT * FROM users ...');
5:Class names (Class names should be capitalized using upper camel case.)
Right:
function BankAccount() {
}
Wrong:
function bank_Account() {
}
6:Constants
Right:
var SECOND = 1 * 1000;

function File() {
}
File.FULL_PERMISSIONS = 0777;
Wrong:
const SECOND = 1 * 1000;

function File() {
}
File.fullPermissions = 0777;
7:Object / Array creation
(Use trailing commas and put short declarations on a single line. Only quote keys when your interpreter complains:)
Right:
var a = ['hello', 'world'];
var b = {
good: 'code',
'is generally': 'pretty',
};
Wrong:
var a = [
'hello', 'world'
];
var b = {"good": 'code'
, is generally: 'pretty'
};
8:Equality operator
Right:
var a = 0;
if (a === '') {
console.log('winning');
}
Wrong:
var a = 0;
if (a == '') {
console.log('losing');
}
9:Extending prototypes
Do not extend the prototypes of any objects, especially native ones. There is a special place in hell waiting for you if you don't obey this rule.
Right:
var a = [];
if (!a.length) {
console.log('winning');
}
Wrong:
Array.prototype.empty = function() {
return !this.length;
}
var a = [];
if (a.empty()) {
console.log('losing');
}
10:Conditions
Any non-trivial conditions should be assigned to a descriptive variable:
任何一个条件都要赋值给一个变量
Right:
var isAuthorized = (user.isAdmin() || user.isModerator());
if (isAuthorized) {
console.log('winning');
}
Wrong:
if (user.isAdmin() || user.isModerator()) {
console.log('losing');
}
11:Function length
尽量保持函数简短 大概10行
Keep your functions short. A good function fits on a slide that the people in the last row of a big room can comfortably read. So don't count on them having perfect vision and limit yourself to ~10 lines of code per function.
12:Return statements

To avoid deep nesting of if-statements, always return a functions value as early as possible.

Right:

function isPercentage(val) {
if (val < 0) {
return false;
}

if (val > 100) {
return false;
}

return true;
}
Wrong:

function isPercentage(val) {
if (val >= 0) {
if (val < 100) {
return true;
} else {
return false;
}
} else {
return false;
}
}
Or for this particular example it may also be fine to shorten things even further:

function isPercentage(val) {
var isInRange = (val >= 0 && val <= 100);
return isInRange;
}
13:Named closures

Feel free to give your closures a name. It shows that you care about them, and will produce better stack traces:

Right:

req.on('end', function onEnd() {
console.log('winning');
});
Wrong:

req.on('end', function() {
console.log('losing');
});
14:Nested Closures

Use closures, but don't nest them. Otherwise your code will become a mess.

Right:

setTimeout(function() {
client.connect(afterConnect);
}, 1000);

function afterConnect() {
console.log('winning');
}
Wrong:

setTimeout(function() {
client.connect(function() {
console.log('losing');
});
}, 1000);

posted on 2014-11-29 23:06  nxp  阅读(109)  评论(0)    收藏  举报