前端

前端 JAVASCRIPT规范

2019-03-06  本文已影响0人  Diogoxiang

目录

1.前言

制定良好的统一的JavaScript代码规范,提高代码质量和团队合作的效率。

2. 注释

    /**
     * 函数描述
     *
     * @param {string} p1 参数1的说明
     * @param {string} p2 参数2的说明,比较长
     * 那就换行了.
     * @param {number=} p3 参数3的说明(可选)
     * @return {Object} 返回值描述
     **/
    function foo(p1, p2, p3) {
        var p3 = p3 || 10;
        return {
            p1: p1,
            p2: p2,
            p3: p3
        };
    }

3.缩进

缩进使用4个空格(四个空格在所有编辑器上显示缩进一致)

4.分号

4.1 在语句(Statement)的结尾加分号

    // bad
    (function() {
      var name = 'Skywalker'
      return name
    })()

    // good
    (function() {
      var name = 'Skywalker';
      return name;
    })();

4.2 这几种情况后需加分号:变量声明 表达式 return throw break continue do-while。

    /* 示例 */
    /* var declaration */
    var x = 1;

    /* expression statement */
    x++;

    /* do-while */
    do {
        x++;
    } while (x < 10);

5.逗号

行首不要出现逗号。

    // bad
    var demo = [
        app
      , icon
      , alive
    ];

    // good
    var demo = [
      app,
      icon,
      alive
    ];

6.引号

最外层统一使用单引号。

    // not good
    var x = "test";

    // good
    var y = 'foo',
    z = '<div id="test"></div>';

7.变量

7.1变量命名

    //示例
    var thisIsMyName;
    //示例
    var thisID;
    //示例
    var reportURL;
    //示例
    var AndroidVersion;
    //示例
    var iOSVersion;
    //示例
    var MAX_COUNT = 10;
    //not good
    var body = $('body');
    // good
    var $body=$('body');

7.2 变量声明

var 语句

    //bad
    count = 10; //严格模式中报错
    console.log(global.count); //10

    //good
    var count = 10;
    // not good
    var uname,
        uid,
        upwd;
    //good  
    var uname;
    var uid='';
    var upwd='';

8.对象

    // bad
    var test = new Object();

    // good
    var test = {};
    // bad
    var superman = {
      class: 'superhero',
      default: { clark: 'kent' },
      private: true
    };

    // good
    var superman = {
      klass: 'superhero',
      defaults: { clark: 'kent' },
      hidden: true
    };

9.数组

    // bad
    var items = new Array();

    // good
    var items = [];
    var arrayList = [];
    // bad
    arrayList[arrayList.length] = '111111';

    // good
    arrayList.push('1111111');
    var len = items.length;
    var itemsCopy = [];
    var i;

    // bad
    for (i = 0; i < len; i++) {
      itemsCopy[i] = items[i];
    }

    // good
    itemsCopy = items.slice();

10.字符串

    // bad
    var test = "sakjas";

    // good
    var test = 'adas dasdas';
    // bad
    var errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';

    // bad
    var errorMessage = 'This is a super long error that was thrown because \
    of Batman. When you stop to think about how Batman had anything to do \
    with this, you would get nowhere \
    fast.';

    // good
    var errorMessage = 'This is a super long error that was thrown because ' +
      'of Batman. When you stop to think about how Batman had anything to do ' +
      'with this, you would get nowhere fast.';

11.比较运算符 和 等号

    // bad
    if (number == 10) {
        // ......
    }

    // good
    if (number === 10) {
        // ......
    }  
    // bad  
    if (name !== '') {  
      // 字符串是否为空
    }

    // good  
    if (name) {  
      // 字符串是否为空
    }

    // bad  
    if (collection.length > 0) {  
      // 数组是否为空  
    }

    // good  
    if (collection.length) {  
      // 数组是否为空  
    }  

了解更多信息在 Truth Equality and JavaScript by Angus Croll.

12.类型转换

    //  => this.num = 2;  

    // bad  
    var total = this.num + '';  

    // good  
    var total = '' + this.num;  

    // bad  
    var total = '' + this.num + ' total score';  

    // good  
    var total = this.num + ' total score';    
    var inputValue = '4';  

    // bad  
    var val = new Number(inputValue);  

    // bad  
    var val = +inputValue;  
    // bad  
    var val = parseInt(inputValue);  

    // good  
    var val = Number(inputValue);  

    // good 
    var val = parseInt(inputValue, 10);  
    var page = 0;  

    // bad  
    var tpage = new Boolean(page);

    // good
    var tpage = Boolean(page);  

    // good    
    var tpage = !!page;  
上一篇 下一篇

猜你喜欢

热点阅读