Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript编码规范 #10

Open
kuitos opened this issue Aug 23, 2015 · 0 comments
Open

Javascript编码规范 #10

kuitos opened this issue Aug 23, 2015 · 0 comments

Comments

@kuitos
Copy link
Owner

kuitos commented Aug 23, 2015

Javascript编码规范

原文写于 2014-10-10

PPT

命名规范

  1. * 所有变量声明必须加var关键字,函数声明使用function关键字,所有声明必须放到作用域的顶端,严格控制作用域;多个变量命名以逗号分隔,每个占一行 。如 var a=0,b=0
  2. 使用驼峰式命名变量和函数,如:functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis,namespaceNamesLikeThis;
  3. 私有成员变量和方法命名以下划线开头,如:var _this;
  4. 常量定义单词全部大写,以下划线连接,但不要用const关键字来声明,如:SOME_CONSTANTS;
  5. 文件名必须全部用小写,文件名分隔符用中划线连接,版本连接符用实心点,合并文件的文件名连接符用下划线,如:passport-core.min.js和reset-1.0_utils-1.0.css;

编码规范

  1. 禁止在代码块中声明函数错误的范例:if (true) {function foo() {}};
  2. 禁止用new来实例化基本类型,错误的范例:var x = new Boolean(false);
  3. 使用数组或对象直接量,不使用new关键字声明,错误的范例:var a = new Array();var o = new Object();
  4. 避免前后端编码切换造成的不适,使用双引号来定义字符串;
  5. 如无特殊必要禁止使用全局变量,所有代码块运行在闭包环境中,且以严格模式运行。如 (function(window){"use strict";})(window);
  6. 禁止使用for in 遍历数组
  7. 禁止使用with(严格模式下已移除with)
  8. 如无特殊情况,禁止使用eval()

angularJS-编码规范

  1. controller统一采用首字母大写的驼峰写法。
  2. 包括但不限于controller、service、directive必须采用标准的依赖注入的写法。
    错误的写法:
    function TestController($scope, $http){
    // do something
    }
    正确的写法:
    app.controller("TestController", ["$scope", "$http", function($scope, $http){
    // do something
    }]);
    错误的写法会导致js压缩时出现js报错。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant