
1. Javascript代码应符合Douban-JSLint检验标准
1-1. 语句必须都有分号结尾,除了for, function, if, switch, try, while
1-2. 只有长语句可以考虑断行,如:
TEMPL_SONGLIST.replace({TABLE}, da[results])
.replace({PREV_NUM}, prev)
.replace({NEXT_NUM}, next)
.replace({CURRENT_NUM}, current)
.replace({TOTAL_NUM}, da.page_total);
为了避免和JSLint的检验机制冲突,“.”或“+”这类操作符放在行尾,上面代码应改为:
TEMPL_SONGLIST.replace({TABLE}, da[results]).
replace({PREV_NUM}, prev).
replace({NEXT_NUM}, next).
replace({CURRENT_NUM}, current).
replace({TOTAL_NUM}, da.page_total);
1-3. 避免额外的逗号。如:var arr = [1,2,3,];
1-4. 所有的循环体和判断体都需要用”{}”括起来。如:
错:
if (condition)
statement;
或
if (condition) statement;
对:
if (condition) {
statement; 或
if (condition) { statement; }
1-5. for-in循环体中必须用hasOwnProperty方法检查成员是否为自身成员。避免来自原型链上的污染。
1-6. 变量声明。变量声明应放在function的最上面。避免使用未声明的变量。
错:
if (n