命名规范

  • 文件及文件夹: 全部英文小写字母+数字或连接符”- , _ “,不可出现其他字符

    如:../css/style.css, jquery.1.x.x.js

  • 文件:调用 /libs 文件需包含版本号,压缩文件需包含min关键词,其他插件则可不包含 如:/libs/jquery.1.9.1.js /libs/ycloud-1.7.min.js fileuploader.js plugins.js

  • ID: 小驼峰式命名
    如:firstName topBoxList footerCopyright
  • Class: [减号连接符]
    如:top-item main-box box-list-item-1
  • 尽量使用语义明确的单词命名,避免 left bottom 等方位性的词语

一律使用小写字母

  1. <!-- Recommended -->
  2. <img src="google.png" alt="Google">
  3. <!-- Not recommended -->
  4. <A HREF="/">Home</A>
  1. /* Recommended */
  2. color: #e5e5e5;
  3. /* Not recommended */
  4. color: #E5E5E5;

省略外链资源 URL 协议部分

省略外链资源(图片及其它媒体资源)URL 中的 http / https 协议,使 URL 成为相对地址,避免Mixed Content 问题,减小文件字节数。

  1. <!-- Recommended -->
  2. <script src="//www.w3cschool.cn/statics/js/autotrack.js"></script>
  3. <!-- Not recommended -->
  4. <script src="http://www.w3cschool.cn/statics/js/autotrack.js"></script>
  1. /* Recommended */
  2. .example {
  3. background: url(//www.google.com/images/example);
  4. }
  5. /* Not recommended */
  6. .example {
  7. background: url(http://www.google.com/images/example);
  8. }

统一注释

HTML注释

  • 模块注释

    1. <!-- 供应商列表模块 -->
    2. <div class="supply-list">
    3. ...
    4. </div>
  • 区块注释

    1. <!--
    2. @name: Drop Down Menu
    3. @description: Style of top bar drop down menu.
    4. @author: zhangwjr(zhangwjr@yonyou.com)
    5. -->

    CSS注释

    组件块和子组件块以及声明块之间使用一空行分隔,子组件块之间三空行分隔;

    1. /* ==========================================================================
    2. 组件块
    3. ============================================================================ */
    4. /* 子组件块
    5. ============================================================================ */
    6. .selector {
    7. padding: 15px;
    8. margin-bottom: 15px;
    9. }
    10. /* 子组件块
    11. ============================================================================ */
    12. .selector-secondary {
    13. display: block; /* 注释*/
    14. }
    15. .selector-three {
    16. display: span;
    17. }

    JS注释

  • 单行注释

必须独占一行。// 后跟一个空格,缩进与下一行被注释说明的代码一致。

  • 多行注释

避免使用 /*...*/ 这样的多行注释。有多行注释内容时,使用多个单行注释。

  • 函数/方法注释

    • 函数/方法注释必须包含函数说明,有参数和返回值时必须使用注释标识。;
    • 参数和返回值注释必须包含类型信息和说明;
    • 当函数是内部函数,外部不可访问时,可以使用 @inner 标识;
      1. /**
      2. * 函数描述
      3. *
      4. * @param {string} p1 参数1的说明
      5. * @param {string} p2 参数2的说明,比较长
      6. * 那就换行了.
      7. * @param {number=} p3 参数3的说明(可选)
      8. * @return {Object} 返回值描述
      9. */
      10. function foo(p1, p2, p3) {
      11. var p3 = p3 || 10;
      12. return {
      13. p1,
      14. p2: p2,
      15. p3: p3
      16. };
      17. }
  • 文件注释

文件注释用于告诉不熟悉这段代码的读者这个文件中包含哪些东西。 应该提供文件的大体内容, 它的作者, 依赖关系和兼容性信息。如下:

  1. /**
  2. * @fileoverview Description of file, its uses and information
  3. * about its dependencies.
  4. * @author user@yonyou.com (Firstname Lastname)
  5. */

对象简写

  1. const name = '采购云';
  2. // bad
  3. const obj = {
  4. name: name
  5. };
  6. // good
  7. const obj = {
  8. name
  9. };

对象解构

  1. // bad
  2. function getFullName(user) {
  3. const firstName = user.firstName;
  4. const lastName = user.lastName;
  5. return `${firstName} ${lastName}`;
  6. }
  7. // good
  8. function getFullName(user) {
  9. const { firstName, lastName } = user;
  10. return `${firstName} ${lastName}`;
  11. }
  12. // best
  13. function getFullName({ firstName, lastName }) {
  14. return `${firstName} ${lastName}`;
  15. }

数组解构

  1. const arr = [1, 2, 3, 4];
  2. // bad
  3. const first = arr[0];
  4. const second = arr[1];
  5. // good
  6. const [first, second] = arr;

每一行的结尾添加分号,当 JavaScript 遇到没有分号结尾的一行,它会执行自动插入分号

  1. // bad
  2. (function () {
  3. const name = 'luka'
  4. return name
  5. })()
  6. // good
  7. (function () {
  8. const name = 'luka';
  9. return name;
  10. }());
  11. // good, 行首加分号,避免文件被连接到一起时立即执行函数被当做变量来执行。
  12. ;(() => {
  13. const name = 'luka';
  14. return name;
  15. }());

推荐使用箭头函数,特殊情况可以使用正常函数

  1. // bad
  2. function foo() {
  3. const self = this;
  4. return function () {
  5. console.log(self);
  6. };
  7. }
  8. // bad
  9. function foo() {
  10. const that = this;
  11. return function () {
  12. console.log(that);
  13. };
  14. }
  15. // good
  16. function foo() {
  17. return () => {
  18. console.log(this);
  19. };
  20. }

Whitespace

tabSize为2

  1. // bad
  2. function foo() {
  3. ∙∙∙∙const name;
  4. }
  5. // bad
  6. function bar() {
  7. const name;
  8. }
  9. // good
  10. function baz() {
  11. ∙∙const name;
  12. }

在大括号前空一格

  1. // bad
  2. function test(){
  3. console.log('test');
  4. }
  5. // good
  6. function test() {
  7. console.log('test');
  8. }
  9. // bad
  10. dog.set('attr',{
  11. age: '1 year',
  12. breed: 'Bernese Mountain Dog',
  13. });
  14. // good
  15. dog.set('attr', {
  16. age: '1 year',
  17. breed: 'Bernese Mountain Dog',
  18. });

在控制语句(if, while 等)的圆括号前空一格

  1. // bad
  2. if(isShow) {
  3. fight ();
  4. }
  5. // good
  6. if (isShow) {
  7. fight();
  8. }

在函数调用和定义时,参数列表和函数名之间不空格

  1. // bad
  2. function fight () {
  3. console.log ('Swooosh!');
  4. }
  5. // good
  6. function fight() {
  7. console.log('Swooosh!');
  8. }

用空格来隔开运算符

  1. // bad
  2. const x=y+5;
  3. // good
  4. const x = y + 5;

每一行的结尾添加分号

  1. // bad
  2. (function () {
  3. const name = 'yonyoucloud'
  4. return name
  5. })()
  6. // good
  7. (function () {
  8. const name = 'yonyoucloud';
  9. return name;
  10. }());
  11. // good, 行首加分号,避免文件被连接到一起时立即执行函数被当做变量来执行。
  12. ;(() => {
  13. const name = 'yonyoucloud';
  14. return name;
  15. }());