其餘規範:
HTML 規範javascript
媒體文件規範前端
CSS 規範vue
javascript 規範java
vue項目規範node
統一兩個空格縮進ajax
局部變量必定要聲明,避免全局污染npm
let
全面代替var
,由於它建立了塊級做用域變量(變量只在代碼塊內生效),尤爲是for
循環const
,它表明常量,定的同時必須賦值使用 string 時,用單引號替代雙引號(寫 JSON 時除外)json
推薦:數組
var foo = 'bar';
var http = require('http');
複製代碼
不推薦:async
var foo = 'bar';
var http = require('http');
複製代碼
變量命名採用小駝峯命名,如:addUser password studentID
常量命名採用單詞全部字母大寫,並用下劃線分隔,如:FORM_NAME
對於對象、函數、和實例採用小駝峯(camelCase)命名法
// 對象
let isObject = {};
// 函數
function isFun(){
...
};
// 實例
let myBbj = new Object();
複製代碼
對於類命名或者構造函數,採用大駝峯命名 User() DateBase()
// 類
class Point {
...
};
// 構造函數
function User(options) {
this.name = options.name;
}
let myBbj = new User({
name: 'yup'
});
複製代碼
在變量做用範圍的最頂端聲明變量
比較操做時必須使用全等符號,'==='和'!=='
在聲明語句的最前端執行強制類型轉換
// -> string:
var totalScore = '' + this.reviewScore;
複製代碼
使用 parseInt
來進行整數的類型轉換,而且始終提供一個基數.
var inputValue = '4';
var val = parseInt(inputValue, 10);
複製代碼
用 {}
,[]
代替 new Array
,new Object
建議使用如下順序來組織 node 代碼中的 require 代碼
1.core modules
2.npm modules
3.others
var http = require('http');
var fs = require('fs');
var async = require('async');
var mongoose = require('mongoose');
var Car = require('./models/Car');
複製代碼
引入模塊時,不要加 .js 後綴
var Batmobil = require('./models/Car');
複製代碼
魔鬼數字主要指在代碼中沒有具體含義的數字、字符串。影響了代碼可讀性,讀者看到的數字沒法理解其含義,從而難以理解程序的意圖。當程序中出現的魔鬼數字過多時,代碼的可維護性將會急劇降低,代碼變得難以修改,並容易引入錯誤。
要求將魔鬼數字定義爲常量,並增長註釋
// 接口成功返回的標識
const SUCCESS_CODE = 1;
// 接口失敗返回的標識
const FAIL_CODE = 0;
$.ajax({
url: baseUrl + 'v1/activity/getAct',
cache: false,
type: 'GET',
dataType: 'json',
success: function(result) {
if (result.ret === SUCCESS_CODE) {
...
}else if(result.ret === FAIL_CODE){
...
}
}
});
複製代碼
從意義上應該是一個獨立的功能模塊
函數名要在必定程度上反映函數的功能
函數名要在必定程度上反映函數的功能
當函數參數不該該在函數體內部被修改時,應加上 const
聲明
避免函數有過多的參數,參數個數儘可能控制在 4 個之內
一個函數只作一件事,就要儘可能保證抽象層級的一致性,全部語句儘可能在一個粒度上。若在一個函數中處理多件事,不利於代碼的重用
在回調函數中要始終檢測錯誤,並返回錯誤
function getAnimals(done) {
Animal.get(function(err, animals) {
if (err) {
// 返回錯誤
return done(err);
}
// 建議回調函數中使用描述性的參數
return done(null, {
dogs: animals.dogs,
cats: animals.cats
});
});
}
複製代碼
在同步調用中多使用 Try catch 去捕獲錯誤,如:JSON.parse()應該使用 try-catch 語句塊
var data;
try {
data = JSON.parse(jsonAsAString);
} catch (e) {
//handle error - hopefully not with a console.log ;)
console.log(e);
}
複製代碼
在原型鏈上增長屬性,而不是覆寫原型鏈
推薦:
function Jedi() {
console.log('new jedi');
}
Jedi.prototype.fight = function fight() {
console.log('fighting');
};
Jedi.prototype.block = function block() {
console.log('blocking');
};
複製代碼
不推薦:
function Jedi() {
console.log('new jedi');
}
Jedi.prototype = {
fight: function fight() {
console.log('fighting');
},
block: function block() {
console.log('blocking');
}
};
複製代碼
使用 class
關鍵字,並使用 extends
關鍵字來繼承
class Queue {
constructor(contents = []) {
this._queue = [...contents];
}
pop() {
const value = this._queue[0];
this._queue.splice(0, 1);
return value;
}
}
class PeekableQueue extends Queue {
peek() {
return this._queue[0];
}
}
複製代碼
在方法中返回 this
以方便鏈式調用
推薦:
class Jedi {
jump() {
this.jumping = true;
return this;
}
setHeight(height) {
this.height = height;
return this;
}
}
const luke = new Jedi();
luke.jump()
.setHeight(20);
複製代碼
不推薦:
Jedi.prototype.jump = function() {
this.jumping = true;
return true;
};
Jedi.prototype.setHeight = function(height) {
this.height = height;
};
const luke = new Jedi();
luke.jump(); // => true
luke.setHeight(20); // => undefined
複製代碼
當你必須使用函數表達式(或傳遞一個匿名函數時),使用箭頭函數符號(可以自動綁定this
到父對象)
推薦:
[1, 2, 3].map((x) => {
return x * x
});
複製代碼
不推薦:
[1, 2, 3].map(function(x) {
return x * x;
});
複製代碼
老是用括號包裹參數,即使只有一個參數
推薦:
let foo = (x) => x + 1;
複製代碼
不推薦:
let foo = x => x + 1;
複製代碼
對於對象、類中的方法,使用加強的對象字面量
推薦:
let foo = {
bar () {
// code
}
}
複製代碼
不推薦:
let foo = {
bar: () => {
// code
}
};
let foo = {
bar: function() {
// code
}
};
複製代碼
...
擴展運算符函數參數永遠不要使用類數組對象 arguments
,使用 ...
操做符來代替
推薦:
function concatenateAll(...args) {
return args.join('');
}
複製代碼
不推薦:
function concatenateAll() {
const args = Array.prototype.slice.call(arguments);
return args.join('');
}
複製代碼
使用 ...
來拷貝數組
推薦:
const itemsCopy = [...items];
複製代碼
不推薦:
const len = items.length;
const itemsCopy = [];
let i;
for (i = 0; i < len; i++) {
itemsCopy[i] = items[i];
}
複製代碼
使用/** ... */
進行多行註釋. 請在大家加入註釋說明,指明參數和返回值的類型
/** * make() returns a new element * based on the passed in tag name * @param <String> tag * @return <Element> element */
複製代碼
使用 //
進行單行註釋. 請用一個新行來添加註釋。並在註釋行前增長一個空行
// is current tab
var active = true;
function getType() {
console.log('fetching type...');
// set the default type to 'no type'
var type = this._type || 'no type';
return type;
}
複製代碼