codecamp

jshint

===, !==代替==, !=

for-in里一定要有 hasOwnProperty 的判断;

不要在内置对象的原型上添加方法,如 Array, Date;

不要在内层作用域的代码里声明了变量,之后却访问到了外层作用域的同名变量;

变量不要先使用后声明;

不要在一句代码中单单使用构造函数,记得将其赋值给某个变量;

不要在同个作用域下声明同名变量;

不要在一些不需要的地方加括号,例:delete(a.b)

不要使用未声明的变量(全局变量需要加到.jshintrc文件的globals属性里面);

不要声明了变量却不使用;

不要在应该做比较的地方做赋值;

debugger 不要出现在提交的代码里;

数组中不要存在空元素;

不要在循环内部声明函数;

不要像这样使用构造函数,例:new function () { ... }, new Object

// not good
if (a == 1) {
    a++;
}


// good
if (a === 1) {
    a++;
}


// good
for (key in obj) {
    if (obj.hasOwnProperty(key)) {
        // be sure that obj[key] belongs to the object and was not inherited
        console.log(obj[key]);
    }
}


// not good
Array.prototype.count = function(value) {
    return 4;
};


// not good
var x = 1;


function test() {
    if (true) {
        var x = 0;
    }


    x += 1;
}


// not good
function test() {
    console.log(x);


    var x = 1;
}


// not good
new Person();


// good
var person = new Person();


// not good
delete(obj.attr);


// good
delete obj.attr;


// not good
if (a = 10) {
    a++;
}


// not good
var a = [1, , , 2, 3];


// not good
var nums = [];


for (var i = 0; i < 10; i++) {
    (function(i) {
        nums[i] = function(j) {
            return i + j;
        };
    }(i));
}


// not good
var singleton = new function() {
    var privateVar;


    this.publicMethod = function() {
        privateVar = 1;
    };


    this.publicMethod2 = function() {
        privateVar = 2;
    };
};
undefined
杂项
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

编辑器配置和构建检查

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }