不管有多少人共同参与同一项目,一定要确保每一行代码都像是同一个人编写的
全部采用 小写 方式,以 下划线 分隔例:my_project_name
例如: scripts、styles、images、data_modals
参照项目命名规则例: lk-utils.js
参照项目命名规则例:lk-style.scss
参照项目命名规则例:lk-index.html
为每个 HTML 页面的第一行添加standards mode(标准模式)声明,这样能够确保在每个浏览器中拥有一致的展现
根据HTML5规范:
应在html标签上加上lang属性,从而为文档设置正确的语言。这会给语音工具和翻译工具帮助,告诉它们应当怎么去发音和翻译。
更多关于 lang 属性的说明在这里 在菜鸟联盟上可以查到语言列表
通过声明一个明确的字符编码,让浏览器轻松、快速的确定适合网页内容的渲染方式,通常指定为‘UTF-8’
如果想要了解更多,请点击这里
What does <meta http-equiv=“X-UA-Compatible” content=“IE=edge”> do? Depending upon what Microsoft browsers you support you may not need to continue using the X-UA-Compatible tag. If you need to support IE9 or IE8, then I would recommend using the tag. If you only support the latest browsers (IE11 and/or Edge) then I would consider dropping this tag altogether.
简而言之需要兼容IE9/8,加上X-UA-Compatible,否则不用加
不同doctype在不同浏览器下会触发不同的渲染模式(这篇文章总结的很到位) <!DOCTYPE html> <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> </head> ... </html>根据html5规范,通常在引入CSS和JS时不需要指明type,因为 text/css和 text/javasctipt 分别是他们的默认值
属性应该按照特定的顺序出现以保证易读性
classidnamedata-*src , for , type , href , value , max-length , max , min , patternplaceholder , title , altaria-* , rolerequired , readonly , disabledclass是为高可复用组件设计的,所以应处在第一位id更加具体且应该尽量少使用,所以将它放在第二位
<a class="..." id="..." data-modal="toggle" href="#">Example link</a> <input class="form-control" type="text"> <img src="..." alt="...">boolean属性指不需要声明取值的属性,XHTML需要每个属性声明取值,但是HTML5中并不需要
boolean属性的存在表示取值为true,不存在则表示取值为false。
<input type="text" disabled> <input type="checkbox" value="1" checked> <select> <option value="1" selected>1</option> </select>在JS文件中生成标签让内容变得更难查找,更难编辑,性能更差。应该尽量避免这种情况的出现。
使用2空格
每个属性声明末尾都要加分号
以下几种情况不需要空格:
每条声明语句的 : 后多个规则的分隔符 " . 前!important ! 后属性值中 ( ) 的前后行末不要有多余的空格以下几种情况需要空格:
属性值前选择器 > , + , ~ 前后{ 前!important ! 前@else 前后属性值中的 , 后注释 /* _后和 __*/_ 前 /* not good */ .element { color :red! important; background-color: rgba(0,0,0,.5); } /* good */ .element { color: red !important; background-color: rgba(0, 0, 0, .5); } /* not good */ .element , .dialog{ ... } /* good */ .element, .dialog { } /* not good */ .element>.dialog{ ... } /* good */ .element > .dialog{ ... } /* not good */ .element{ ... } /* good */ .element { ... } /* not good */ @if{ ... }@else{ ... } /* good */ @if { ... } @else { ... }以下几种情况需要空行:
文件最后保留一个空行} 后最好跟一个空行,包括SCSS中嵌套的规则属性组之间需要适当的空行,详见下方的 属性顺序 /* not good */ .element { ... } .dialog { color: red; &:after { ... } } /* good */ .element { ... } .dialog { color: red; &:after { ... } }以下几种情况不需要换行:
{ 前以下几种情况需要换行:
{ 后和 } 前每个属性独占一行多个规则的分隔符 , 后 /* not good */ .element {color: red; background-color: black;} /* good */ .element { color: red; background-color: black; } /* not good */ .element, .dialog { ... } /* good */ .element, .dialog { ... }扩展阅读:
Scope CSS classes with prefixesStop the cascade完整的属性列表及其排列顺序请参考 Bootstrap property order for Stylelint
.declaration-order { /* Positioning */ position: absolute; top: 0; right: 0; bottom: 0; left: 0; z-index: 100; /* Box-model */ display: block; float: right; width: 100px; height: 100px; /* Typography */ font: normal 13px "Helvetica Neue", sans-serif; line-height: 1.5; color: #333; text-align: center; /* Visual */ background-color: #f5f5f5; border: 1px solid #e5e5e5; border-radius: 3px; /* Misc */ opacity: 1; }常见的属性简写包括:
fontbackgroundtransitionanimation /* not good */ .element { transition: opacity 1s linear 2s; } /* good */ .element { transition-delay: 2s; transition-timing-function: linear; transition-duration: 1s; transition-property: opacity; }使用sass的@import规则并不需要指明被导入文件的全名。你可以省略.sass或.scss文件后缀。 当你@import一个局部文件时,还可以不写文件的全名,即省略文件名开头的下划线。
嵌套最多不能超过5层@extend 中使用placeholder选择器去掉不必要的父级引用符号 &操作符为了提高可读性,在圆括号中的数学计算表达式的数值、变量和操作符之间均添加一个空格
/* not good */ @import "_dialog.scss"; /* good */ @import "dialog"; /* not good */ .fatal { @extend .error; } /* good */ .fatal { @extend %error; } /* not good */ .element { & > .dialog { ... } } /* good */ .element { > .dialog { ... } } /* not good */ .element { margin: 10px 0 @variable*2 10px; } /* good */ .element { margin: 10px 0 (@variable * 2) 10px; }详情在这里
尽量少用 * 选择器 /* not good */ .selector, .selector-secondary, .selector[type=text] { padding:15px; margin:0px 0px 15px; background-color:rgba(0, 0, 0, 0.5); box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF } /* good */ .selector, .selector-secondary, .selector[type="text"] { padding: 15px; margin-bottom: 15px; background-color: rgba(0,0,0,.5); box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff; }以下变量全部使用var来声明,开发中请用let、const代替
使用2空格
一下几种情况后需加分号:
变量声明表达式returnthrowbreakcontinuedo-while /* var declaration */ var x = 1; /* expression statement */ x++; /* do-while */ do { x++; } while (x < 10);以下几种情况不需要空格:
对象的属性的属性名后 // not good var a = { b :1 }; // good var a = { b: 1 }; 前缀一元运算符后,后缀一元运算符前函数调用括号前无论是函数声明还是函数表达式, ( 前不要空格数组的 [ 后和 ] 前对象的 { 后和 } 前运算符 { 后和 } 前以下几种情况需要空格:
二元运算符前后三元运算符 ? : 前后代码块 { 前下列关键字前: else , while, catch, finally下列关键字后:if, else, for, while, do, switch, case, try, catch, finally, with, return, typeof单行 // 后(若单行注释和代码同行,则 // 前也需要),多行注释 * 后对象的属性值前for循环,分号后留有一个空格,前置条件如果有多个,逗号后留一个空格无论是函数声明还是函数表达式, { 前一定要有空格函数的参数之间以下几种情况需要空行:
变量声明后(当变量声明在代码块的最后一行时,则无需空行)注释前(当注释在代码块的第一行时,无需空行)代码块后(在函数调用、数组、对象中则无需空行)文件最后保留一个空行 // 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; } // not need blank linße 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 };换行的地方,行末必须有 , 或者运算符以下几种情况不需要换行:
下列关键字后:else, catch, finally代码块 { 前以下几种情况需要换行:
代码块 { 后和 } 前变量赋值后 换行的地方,行末必须有','或者运算符; 以下几种情况不需要换行: 下列关键字后: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;最少三行, * 后跟一个空格,具体参照代码示例的写法建议在以下情况下使用:
难于理解的代码段可能存在错误的代码段浏览器特殊的HACK代码业务逻辑强相关的代码 /* * one space after '*' */ var x = 1;各类标签 @param , @method 等描述和例子请参考usejsdoc和JSDoc Guide(重要)建议在以下情况下使用:
所有常量所有函数所有类 /** * @func * @desc 一个带参数的函数 * @param {string} a - 参数a * @param {number} b=1 - 参数b默认值为1 * @param {string} c=1 - 参数c有两种支持的取值</br>1—表示x</br>2—表示xx * @param {object} d - 参数d为一个对象 * @param {string} d.e - 参数d的e属性 * @param {string} d.f - 参数d的f属性 * @param {object[]} g - 参数g为一个对象数组 * @param {string} g.h - 参数g数组中一项的h属性 * @param {string} g.i - 参数g数组中一项的i属性 * @param {string} [j] - 参数j是一个可选参数 */ function foo(a, b, c, d, g, j) { ... }最外层统一使用单引号
// not good var x = "test"; // good var y = 'foo', z = '<div id="test"></div>';变量声明
一个函数作用域中所有的变量声明尽量提到函数首部用一个var声明,不允许出现两个连续的var声明 function doSomethingWithItems(items) { // use one var var value = 10, result = value + 10, i, len; for (i = 0, len = items.length; i < len; i++) { result += 10; } }下列关键字后必须有大括号(即使代码块的内容只有一行):if, else, for, while, do, switch, try, catch, finally, with
// not good if (condition) doSomething(); // good if (condition) { doSomething(); }适用场景:
初始化一个将来可能被赋值为对象的变量与已经初始化的变量做比较作为一个参数为对象的函数的调用传参作为一个返回对象的函数的返回值不适用场景:
不要用null来判断函数调用时有无传参(有可能是undefined?)不要与未初始化的变量做比较 // 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) { ... }将你的编辑器按照下面的配置进行设置,以避免常见的代码不一致和差异:
用两个空格代替制表符(soft-tab 即用空格代表 tab 符)。保存文件时,删除尾部的空白符。设置文件编码为 UTF-8。在文件结尾添加一个空白行。参照文档并将这些配置信息添加到项目的 .editorconfig 文件中。例如:Bootstrap 中的 .editorconfig 实例。更多信息请参考 about EditorConfig。
具体参考JSDoc Guide
具体参考vue代码风格介绍
主要参考以下文章
Code Guide by @AlloyTeam编码规范 by @mdo感谢以上团队或个人