zoukankan      html  css  js  c++  java
  • javascript代码规范

    1. Javascript代码应符合Douban-JSLint检验标准

    1-1. 语句必须都有分号结尾,除了for, function, if, switch, try, while

    1-2. 只有长语句可以考虑断行,如:

                                     TEMPL_SONGLIST.replace('{TABLE}', da['results'])
                                                   .replace('{PREV_NUM}', prev)
                                                   .replace('{NEXT_NUM}', next)
                                                   .replace('{CURRENT_NUM}', current)
                                                   .replace('{TOTAL_NUM}', da.page_total);

    为了避免和JSLint的检验机制冲突,“.”或“+”这类操作符放在行尾,上面代码应改为:

                                     TEMPL_SONGLIST.replace('{TABLE}', da['results']).
                                                   replace('{PREV_NUM}', prev).
                                                   replace('{NEXT_NUM}', next).
                                                   replace('{CURRENT_NUM}', current).
                                                   replace('{TOTAL_NUM}', da.page_total);

    1-3. 避免额外的逗号。如:var arr = [1,2,3,];

    1-4. 所有的循环体和判断体都需要用"{}"括起来。如:

    错:

                                     if (condition)
                                         statement;
                                     或
                                      if (condition) statement;
                                     

    对:

                                     if (condition) {
                                         statement;                                  或
                                     if (condition) { statement; }

    1-5. for-in循环体中必须用hasOwnProperty方法检查成员是否为自身成员。避免来自原型链上的污染。

    1-6. 变量声明。变量声明应放在function的最上面。避免使用未声明的变量。

    错:

                                     if (n > 0) {
                                       var isvalid = true;                                  

    对:

                                     var isvalid;
                                     if (n > 0) {
                                       isvalid = true;
                                     }

    1-7. 不要使用with, void, evil。

    1-8. 使用严格的条件判断符。用===代替==,用!==代替!=。

    1-9. 下面类型的对象不建议用new构造:new Number, new String, new Boolean, new Object(用{}代替), new Array(用[]代替)。

    1-10. 引用对象成员用obj.prop1代替obj[“prop1”],除非属性名是变量。

    注:Douban-JSLint是定制过的JSLint

    注:如果模块代码中,使用其它全局变量想跳过JSLint的检查,可以在该文件中加入声明,如:

    2. Javascript命名规则

    2-1. 构造器的首字母大写。如:

                             function Dialog (config) {
                               statement;
                             }                          var dlg = new Dialog({...});

    2-2. 对象的属性或方法名采用小驼峰式(lower camel-case),如"init", "bindEvent", "updatePosition":

                             Dialog.prototype = {
                               init: function () {},
                               bindEvent: function () {},
                               updatePosition: function () {}                          };

    2-3. 私有变量名用下划线开头。如:"_current", "_defaultConfig"

    2-4. 常量名全部大写,单词间用下划线分隔。如:“CSS_BTN_CLOSE”, "TXT_LOADING"

    2-5. 变量名的前缀:

    Prefix

    Element

    Example

    integer

    nVariableName

    i,j,k,m,n, etc. *

    integer as counter/iterator

    (for i=0; i<=oArray.length; i++)

    string

    sVariableName

    object

    oObjectName

    is, can, has

    boolean

    [Boolean name]ConditionName

    event method

    event attachment

    [event type]_MethodName

    accessor method

    getMethodName

    accessor method

    setMethodName

    Note: Only a counter/iterator should use a single-letter designation.

    3. 代码格式化要求

    3-1. 语句中的必要空格和缩进

    3-1-1. 用来包含语句的"()"前后需要跟空格,诸如: if / for / while / switch ( statements ) { … } 等

    3-1-2. "="前后需要跟空格

    3-1-3. 数组成员间的","后面需要跟空格

    不好:

                 for (t in selected) { if (!hash[t]) deselect(t) }

    好:

                 for ( t in selected ) {
                   if ( !hash[t] ) {
                    deselect(t);              }

    3-2. 长语句采用断行:

    不好:

                 TEMPL_SONGLIST.replace('{TABLE}', da['results']).replace('{PREV_NUM}', prev).replace('{NEXT_NUM}', next).replace('{CURRENT_NUM}', current).replace('{TOTAL_NUM}', da.page_total);

    好:

                 TEMPL_SONGLIST.replace('{TABLE}', da['results']).
                               replace('{PREV_NUM}', prev).
                               replace('{NEXT_NUM}', next).
                               replace('{CURRENT_NUM}', current).
                               replace('{TOTAL_NUM}', da.page_total);

    3-3. 格式化对象参数:

    不好:

                  embedSWF(id, { url: '/swf/player30792.swf?url=' + el.href, 261, height: 30, params: { wmode:'transparent' }, attributes: { id: "player-sample" + i, name: "player-sample" + i }});

    好:

                  embedSWF(id, {
                     url: '/swf/player30792.swf?url=' + el.href,
                      261,
                     height: 30,
                     params: { wmode:'transparent' },
                     attributes: {
                       id: "player-sample" + i,
                       name: "player-sample" + i                });

  • 相关阅读:
    Spring基础知识
    Hibernate基础知识
    Struts2基础知识
    在eclipse里头用checkstyle检查项目出现 File contains tab characters (this is the first instance)原因
    java后台获取cookie里面值得方法
    ckplayer 中的style.swf 中的 style.xml 中的修改方法
    java hql case when 的用法
    Windows下Mongodb安装及配置
    Mongodb中经常出现的错误(汇总)child process failed, exited with error number
    Mac 安装mongodb
  • 原文地址:https://www.cnblogs.com/blue86/p/2254257.html
Copyright © 2011-2022 走看看