概述
欢迎使用前端代码规范
,前端代码规范
是基于 W3C、 参考 腾讯alloyteam团队、京东凹凸实验室 等前端开发文档,并结合团队日常业务需求以及团队在日常开发过程中总结提炼出的经验而制定。
旨在增强团队开发协作、提高代码质量和打造开发基石的编码规范。
以下规范是团队基本约定的内容,必须严格遵循。
命名规范标题
目录命名,HTML文件命名,JS文件命名,CSS、less、sass文件命名
从 目录、图片、HTML/CSS/JS文件 的命名等层面约定规范团队的命名习惯,增强团队代码的可读性。
参照本项目原有文件命名规范进行命名
HTML 规范
基于 W3C 等官方文档,并结合团队业务和开发过程中总结的规范约定,让页面HTML代码更具语义性。
语法:
- 缩进使用 tab(4 个空格);
- 嵌套的节点应该缩进;
- 在属性上,使用双引号,不要使用单引号;
- 属性名全小写,用中划线(-)做分隔符;
- 要在自动闭合标签结尾处使用斜线;
标准模式
在开头规定 doctype,来启动标准模式,doctype 要大写。
<!DOCTYPE html>
<html>
...
</html>
规定字符编码
通过声明一个明确的字符编码,让浏览器轻松、快速的确定适合网页内容的渲染方式,团队指定为’UTF-8’。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="UTF-8" />
</head>
...
</html>
IE 兼容模式
用 meta 标签指定页面应该使用什么版本的 IE 来渲染。参照原来项目编写
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
</head>
减少标签数量
在编写 HTML 代码时,需要尽量避免多余的父节点;
<!-- bad -->
<span class="avatar">
<img src="..." />
</span>
<!-- good -->
<img class="avatar" src="..." />
语义化标签
html 的标签能使用语义化的,尽量使用语义化标签,避免一个页面都是 div 或者 p 标签
<!-- bad -->
<div>
<p></p>
</div>
<!-- good -->
<header></header>
<footer></footer>
CSS 规范
缩进
使用 tab 缩进(4 个空格),属性冒号 :
后加空格
.element {
border-radius: 10px;
width: 50px;
height: 50px;
}
css选择器
- 不使用通用选择器
*
- 尽量使用class选择器
- 不使用无具体语义定义的标签选择器
/* good */
.jdc {}
.jdc li {}
.jdc li p {}
/* not good */
*{}
#jdc {}
.jdc div{}
分号
每个声明结束都要加分号
.element {
border-radius: 10px;
width: 50px;
height: 50px;
}
注释
注释统一使用 /* */,注释内容与*
有空格
.element {
/* border-radius: 10px; */
width: 50px;
height: 50px;
}
引号
url 的内容要用引号
属性选择器中的属性值需要引号
.element:after {
content: "";
background-image: url("logo.png");
}
li[data-type="single"] {
...;
}
命名
类名使用小写字母,以中划线分隔(待定,参考项目)
id 采用驼峰式命名
scss 中的变量、函数、混合、placeholder 采用驼峰式命名
/* class */
.element-content {
...;
}
/* id */
#myDialog {
...;
}
/* 变量 */
$colorBlack: #000;
/* 混合 */
@mixin centerBlock {
...;
}
代码易读性
左括号与类名之间一个空格,冒号与属性值之间一个空格
/* good */
.jdc {
width: 100%;
}
/* not good */
.jdc{
width:100%;
}
逗号分隔的取值,逗号之后一个空格
/* good */
.jdc {
box-shadow: 1px 1px 1px #333, 2px 2px 2px #ccc;
}
/* not good */
.jdc {
box-shadow: 1px 1px 1px #333,2px 2px 2px #ccc;
}
为单个css选择器或新申明开启新行
/* good */
.jdc,
.jdc_logo,
.jdc_hd {
color: #ff0e0e;
}
.nav {
color: #e3e3e3;
}
/* not good */
.jdc,jdc_logo,.jdc_hd {
color: #ff0e0e;
}.nav{
color: #e3e3e3;
}
颜色属性值十六进制数值统一使用小写
/* good */
.jdc {
color: #fefefe;
}
/* not good */
.jdc {
color: #FEFEFE;
}
不要为 0 指明单位
/* good */
.jdc {
margin: 0 10px;
}
/* not good */
.jdc {
margin: 0px 10px;
}
属性书写顺序
建议遵循以下顺序:
- 布局定位属性:display / position / float / clear / visibility / overflow
- 自身属性:width / height / margin / padding / border / background
- 文本属性:color / font / text-decoration / text-align / vertical-align / white- space / break-word
- 其他属性(CSS3):content / cursor / border-radius / box-shadow / text-shadow / background:linear-gradient …
.jdc {
display: block;
position: relative;
float: left;
width: 100px;
height: 100px;
margin: 0 10px;
padding: 20px 0;
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
color: #333eee;
background: #ffffff;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-o-border-radius: 10px;
-ms-border-radius: 10px;
border-radius: 10px;
}
CSS3浏览器私有前缀写法
CSS3 浏览器私有前缀在前,标准前缀在后
.jdc {
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-o-border-radius: 10px;
-ms-border-radius: 10px;
border-radius: 10px;
}
JavaScript规范
统一团队的 JS 语法风格和书写习惯,减少程序出错的概率,使代码更整洁,可读性更强。
1)缩进
使用soft tab(4个空格)。
var x = 1,
y = 1;
if (x < y) {
x += 10;
} else {
x += 1;
}
2)分号
以下几种情况后需加分号:
- 变量声明
- 表达式
- return
- throw
- break
- continue
- do-while
/* var declaration */
var x = 1;
/* expression statement */
x++;
/* do-while */
do {
x++;
} while (x < 10);
3)空格
以下几种情况不需要空格:
- 对象的属性名后
- 前缀一元运算符后
- 后缀一元运算符前
- 函数调用括号前
- 无论是函数声明还是函数表达式,’('前不要空格
- 数组的’[‘后和’]'前
- 对象的’{‘后和’}'前
- 运算符’(‘后和’)'前
以下几种情况需要空格:
- 二元运算符前后
- 三元运算符’?:'前后
- 逗号后
- 代码块’{'前
- 下列关键字前:else, while, catch, finally
- 下列关键字后:if, else, for, while, do, switch, case, try,catch, finally, with, return, typeof
- 单行注释’//‘后(若单行注释和代码同行,则’//‘前也需要),多行注释’*'后
- 对象的属性值前
- for循环,分号后留有一个空格,前置条件如果有多个,逗号后留一个空格
- 无论是函数声明还是函数表达式,’{'前一定要有空格
- 函数的参数之间
// not good
var a = {
b :1
};
// good
var a = {
b: 1
};
// not good
++ x;
y ++;
z = x?1:2;
// good
++x;
y++;
z = x ? 1 : 2;
// not good
var a = [ 1, 2 ];
// good
var a = [1, 2];
// not good
var a = ( 1+2 )*3;
// good
var a = (1 + 2) * 3;
// no space before '(', one space before '{', one space between function parameters
var doSomething = function(a, b, c) {
// do something
};
// no space before '('
doSomething(item);
// not good
for(i=0;i<6;i++){
x++;
}
// good
for (i = 0; i < 6; i++) {
x++;
}
4)空行
以下几种情况需要空行:
- 变量声明后(当变量声明在代码块的最后一行时,则无需空行)
- 注释前(当注释在代码块的第一行时,则无需空行)
- 代码块后(在函数调用、数组、对象中则无需空行)
- 文件最后保留一个空行
示例
// need blank line after variable declaration
var x = 1;
// not need blank line when variable declaration is last expression in the current block
if (x >= 1) {
var y = x + 1;
}
var a = 2;
// need blank line before line comment
a++;
function b() {
// not need blank line when comment is first line of block
return a;
}
// need blank line after blocks
for (var i = 0; i < 2; i++) {
if (true) {
return false;
}
continue;
}
var obj = {
foo: function() {
return 1;
},
bar: function() {
return 2;
}
};
// not need blank line when in argument list, array, object
func(
2,
function() {
a++;
},
3
);
var foo = [
2,
function() {
a++;
},
3
];
var foo = {
a: 2,
b: function() {
a++;
},
c: 3
};
5)换行
换行的地方,行末必须有’,'或者运算符;
以下几种情况不需要换行:
- 下列关键字后:
else, catch, finally
- 代码块’{'前
以下几种情况需要换行:
- 代码块’{‘后和’}'前
- 变量赋值后
// not good
var a = {
b: 1
, c: 2
};
x = y
? 1 : 2;
// good
var a = {
b: 1,
c: 2
};
x = y ? 1 : 2;
x = y ?
1 : 2;
// no need line break with 'else', 'catch', 'finally'
if (condition) {
...
} else {
...
}
try {
...
} catch (e) {
...
} finally {
...
}
// not good
function test()
{
...
}
// good
function test() {
...
}
// not good
var a, foo = 7, b,
c, bar = 8;
// good
var a,
foo = 7,
b, c, bar = 8;
6)单行注释
- 注释单独一行的情况下,注释的//后面要跟一个空格
- 缩进与下一行代码保持一致;
- 可位于一个代码行的末尾,与代码间隔一个空格。
if (condition) {
// if you made it here, then all security checks passed
allowed();
}
var zhangsan = 'zhangsan'; // one space after code
7)多行注释
最少三行, '*'后跟一个空格,具体参照示例的写法;
建议在以下情况下使用:
- 难于理解的代码段
- 可能存在错误的代码段
- 浏览器特殊的HACK代码
- 业务逻辑强相关的代码
示例
/*
* 代码注释1
* 代码注释2
*/
var x = 1;
8)函数注释
复杂的函数,所有类,都必须进行函数注释,函数注释使用业界统一的规范,方便后续使用 jsdoc 生成文档。
示例
/**
* 获取任务的名称
* @param id {Number} 传入需要获取名称的人物id
* @return {String} 返回的姓名
* @author XXX 2020/01/19 可以不写
* @version 1.1.0 可以不写
* @example 示例代码,可以不写
*/
function getTaskName(id) {
var name = 'test';
return name;
}
9)引号
最外层统一使用单引号。
// not good
var x = "test";
// good
var y = 'foo',
z = '<div id="test"></div>';
10)变量命名
- 标准变量采用驼峰式命名(除了对象的属性外,主要是考虑到cgi返回的数据)
- 'ID’在变量名中全大写
- 'URL’在变量名中全大写
- 'Android’在变量名中大写第一个字母
- 'iOS’在变量名中小写第一个,大写后两个字母
- 常量全大写,用下划线连接
- 构造函数,大写第一个字母
- jquery对象必须以’$'开头命名
示例
var thisIsMyName;
var goodID;
var reportURL;
var AndroidVersion;
var iOSVersion;
var MAX_COUNT = 10;
function Person(name) {
this.name = name;
}
// not good
var body = $('body');
// good
var $body = $('body');
11)函数
- 无论是函数声明还是函数表达式,’(‘前不要空格,但’{'前一定要有空格;
- 函数调用括号前不需要空格;
- 立即执行函数外必须包一层括号;
- 不要给inline function命名;
- 参数之间用’, '分隔,注意逗号后有一个空格。
示例
// no space before '(', but one space before'{'
var doSomething = function(item) {
// do something
};
function doSomething(item) {
// do something
}
// not good
doSomething (item);
// good
doSomething(item);
// requires parentheses around immediately invoked function expressions
(function() {
return 1;
})();
// not good
[1, 2].forEach(function x() {
...
});
// good
[1, 2].forEach(function() {
...
});
// not good
var a = [1, 2, function a() {
...
}];
// good
var a = [1, 2, function() {
...
}];
// use ', ' between function parameters
var doSomething = function(a, b, c) {
// do something
};
12)数组、对象
- 对象属性名不需要加引号;
- 对象以缩进的形式书写,不要写在一行;
- 数组、对象最后不要有逗号。
示例
// not good
var a = {
'b': 1
};
var a = {b: 1};
var a = {
b: 1,
c: 2,
};
// good
var a = {
b: 1,
c: 2
};
- 使用
.
访问对象属性
var joke = {
name: 'haha',
age: 28
}
// not good
var name = joke['name']
// good
var name = joke.name
13)括号
下列关键字后必须有大括号(即使代码块的内容只有一行):if, else,for, while, do, switch, try, catch, finally, with
。
// not good
if (condition)
doSomething();
// good
if (condition) {
doSomething();
}
14)null
适用场景:
- 初始化一个将来可能被赋值为对象的变量
- 与已经初始化的变量做比较
- 作为一个参数为对象的函数的调用传参
- 作为一个返回对象的函数的返回值
不适用场景:
- 不要用null来判断函数调用时有无传参
- 不要与未初始化的变量做比较
示例
// not good
function test(a, b) {
if (b != null) {
// not mean b is not supply
...
}
}
var a;
if (a != null) {
...
}
// good
var a = null;
if (a != null) {
...
}
15)undefined
永远不要直接使用undefined
进行变量判断;
使用typeof
和字符串'undefined'
对变量进行判断。
// not good
if (person === undefined) {
...
}
// good
if (typeof person === 'undefined') {
...
}
16)jshint
- for-in里一定要有
hasOwnProperty
的判断(可以启用ESLint 的 guard-for-in 选项进行判断); - 不要在内置对象的原型上添加方法,如Array, Date;
- 变量不要先使用后声明;
- 不要在一句代码中单单使用构造函数,记得将其赋值给某个变量;
- 不要在同个作用域下声明同名变量;
- 不要在一些不需要的地方加括号,例:
delete(a.b)
; - 不要声明了变量却不使用(sonar扫描会报错);
- debugger不要出现在提交的代码里;
- 数组中不要存在空元素;
- 不要在循环内部声明函数;
- 不要像这样使用构造函数,例:new function () { … }, new Object;
示例
// good
for (key in obj) {
if (obj.hasOwnProperty(key)) {
// be sure that obj[key] belongs to the object and was not inherited
console.log(obj[key]);
}
}
// not good
Array.prototype.count = function(value) {
return 4;
};
// not good
var x = 1;
function test() {
if (true) {
var x = 0;
}
x += 1;
}
// not good
function test() {
console.log(x);
var x = 1;
}
// not good
new Person();
// good
var person = new Person();
// not good
delete(obj.attr);
// good
delete obj.attr;
// not good
var a = [1, , , 2, 3];
// not good
var nums = [];
for (var i = 0; i < 10; i++) {
(function(i) {
nums[i] = function(j) {
return i + j;
};
}(i));
}
// not good
var singleton = new function() {
var privateVar;
this.publicMethod = function() {
privateVar = 1;
};
this.publicMethod2 = function() {
privateVar = 2;
};
};
17)杂项
- 不要混用tab和space;
- 不要在一处使用多个tab或space;
- 换行符统一用’LF’;
- 对上下文this的引用团队约定使用’_this’, ‘that’, 'self’其中一个来命名;
- 行尾不要有空白字符;
switch
的falling through
和no default
的情况一定要有注释特别说明;- 不允许有空的代码块。
示例
// not good
var a = 1;
function Person() {
// not good
var me = this;
// good
var _this = this;
// good
var that = this;
// good
var self = this;
}
// good
switch (condition) {
case 1:
case 2:
...
break;
case 3:
...
// why fall through
case 4
...
break;
// why no default
}
// not good with empty block
if (condition) {
}
条件判断和循环
条件判断能使用三目运算符和逻辑运算符解决的,就不要使用条件判断,但是谨记不要写太长的三目运算符。
// bad
if (x === 10) {
return 'valid';
} else {
return 'invalid';
}
// good
return x === 10 ? 'valid' : 'invalid';
// bad
if (!x) {
if (!y) {
x = 1;
} else {
x = y;
}
}
// good
x = x || y || 1;
eval()
不要使用eval()