js中(!exp)

var exp = null;
if (!exp && typeof exp != "undefined" && exp != 0)
{
    alert("is null");
}
typeof exp != "undefined" 排除了 undefined;
exp != 0 排除了數字零和 false。
 
更簡單的正確的方法:
 
var exp = null;
if (exp === null)
{
    alert("is null");
}
 
--------------------------------------------------------------------------------
 
儘管如此,我們在 DOM 應用中,一般只需要用 (!exp) 來判斷就可以了,因爲 DOM 應用中,可能返回 null,可能返回 undefined,如果具體判斷 null 還是 undefined 會使程序過於複雜。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章