- jshint
jshint
用'===', '!=='代替'==', '!=';
for-in里一定要有hasOwnProperty的判断;
不要在内置对象的原型上添加方法,如Array, Date;
不要在内层作用域的代码里声明了变量,之后却访问到了外层作用域的同名变量;
变量不要先使用后声明;
不要在一句代码中单单使用构造函数,记得将其赋值给某个变量;
不要在同个作用域下声明同名变量;
不要在一些不需要的地方加括号,例:delete(a.b);
不要使用未声明的变量(全局变量需要加到.jshintrc文件的globals属性里面);
不要声明了变量却不使用;
不要在应该做比较的地方做赋值;
debugger不要出现在提交的代码里;
数组中不要存在空元素;
不要在循环内部声明函数;
不要像这样使用构造函数,例:new function () { … }, new Object;
// not goodif (a == 1) {a++;}// goodif (a === 1) {a++;}// goodfor (key in obj) {if (obj.hasOwnProperty(key)) {// be sure that obj[key] belongs to the object and was not inheritedconsole.log(obj[key]);}}// not goodArray.prototype.count = function(value) {return 4;};// not goodvar x = 1;function test() {if (true) {var x = 0;}x += 1;}// not goodfunction test() {console.log(x);var x = 1;}// not goodnew Person();// goodvar person = new Person();// not gooddelete(obj.attr);// gooddelete obj.attr;// not goodif (a = 10) {a++;}// not goodvar a = [1, , , 2, 3];// not goodvar nums = [];for (var i = 0; i < 10; i++) {(function(i) {nums[i] = function(j) {return i + j;};}(i));}// not goodvar singleton = new function() {var privateVar;this.publicMethod = function() {privateVar = 1;};this.publicMethod2 = function() {privateVar = 2;};};
