日期:2014-05-16  浏览次数:20380 次

Js 判别对象 Undefined 和 Null

Js中判别一个目标是underfined 仍是 Null一向仍是对比重要的,先来看看Null:

仿制代码
var exp=""; if (exp.abc == null)//条件是有必要存在exp这个目标,而关于它这个特点,则可进行判别,看是不是为NUll {
    alert("is null");
} if(exp.abc==undefined)
{  
    alert("undefined");//exp 为 undefined 时,也会得到与 null 相同的成果,尽管 null 和 undefined 不相同。注意:要一起判别 null 和 undefined 时可运用本法。 }
仿制代码

?

?

if(exp.ab()==null){ //这儿是错的,会提示说没有这个ab()的办法,所以犯错 alert("function is null");
} if(exp.ab()==undefined){ //这儿也是错的 alert("undefined");
}

?

if(!exp){ //当若是 exp 为 undefined,或字符体""、或null,或数字零,或 false,也会得到与 null 相同的成果,尽管 null 和二者不相同。注意:要一起判别 null、undefined、数字零、false 时可运用本法。 alert("!exp");
}