isEmpty(obj){
var regu = "^[ ]+$"
var re = new RegExp(regu)
if(typeof obj == "undefined" || obj == null || obj == "" || re.test(obj)){
return true
}else{
return false
}
},
用法:
if(this.isEmpty(this.keyword)){
console.log('空字符')
}
空字符串等价于逻辑假,所以直接判断即可:var s=""
if(s){
//s不是空字符串
}else{
//s是空字符串
}
一般判断为空有 null值、undefined值与NaN值判断undefined:
var tmp = undefinedif (typeof(tmp) == "undefined"){ alert("undefined")}
说明:typeof 返回的是字符串,有六种可能:"number"、"string"、"boolean"、"object"、"function"、"undefined"
判断null:
var tmp = nullif (!tmp && typeof(tmp)!="undefined" && tmp!=0){ alert("null")}
判断NaN:
var tmp = 0/0if(isNaN(tmp)){ alert("NaN")}
附上全部相等图≅:松散等于等于检查(==), 比如: "1" == true[] =="0"
=:全等或恒等全等检查(===)