codecamp

null

适用场景

  • 初始化一个将来可能被赋值为对象的变量
  • 与已经初始化的变量做比较
  • 作为一个参数为对象的函数的调用传参
  • 作为一个返回对象的函数的返回值

不适用场景

  • 不要用null来判断函数调用时有无传参
  • 不要与未初始化的变量做比较

示例代码

// not good
function test(a, b) {
    if (b === null) {
        // not mean b is not supply
        ...
    }
}


var a;


if (a === null) {
    ...
}


// good
var a = null;


if (a === null) {
    ...
}
括号
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; }