欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript中2個感嘆號的用法實例詳解

 更新時間:2014年09月04日 16:01:45   投稿:shichen2014  
這篇文章主要介紹了javascript中2個感嘆號的用法,并用大量的實例講述了!!的常見應(yīng)用情況,是非常實用的技巧,需要的朋友可以參考下

在javascript代碼中經(jīng)常會見到!!的情況,本文即以實例形式較為深入的分析javascript中2個感嘆號的用法。分享給大家供大家參考之用。具體分析如下:

javascript中的!!是邏輯"非非",即是在邏輯“非”的基礎(chǔ)上再"非"一次。通過!或!!可以將很多類型轉(zhuǎn)換成bool類型,再做其它判斷。

一、應(yīng)用場景:判斷一個對象是否存在

假設(shè)有這樣一個json對象:

{ color: "#E3E3E3", "font-weight": "bold" }

需要判斷是否存在,用!!再好不過。

如果僅僅打印對象,無法判斷是否存在:

var temp = { color: "#A60000", "font-weight": "bold" };
alert(temp);

結(jié)果:[object: Object]

如果對json對象實施!或!!,就可以判斷該json對象是否存在:

var temp = { color: "#A60000", "font-weight": "bold" };
alert(!temp);

結(jié)果:false

var temp = { color: "#A60000", "font-weight": "bold" };
alert(!!temp);

結(jié)果:true

二、通過!或!!把各種類型轉(zhuǎn)換成bool類型的慣例

1.對null的"非"返回true

var temp = null;
alert(temp); 

結(jié)果:null

var temp = null;
alert(!temp); 

結(jié)果:true

var temp = null;
alert(!!temp); 

結(jié)果:false

2.對undefined的"非"返回true

var temp;
alert(temp);

結(jié)果:undefined

var temp;
alert(!temp);

結(jié)果:true

var temp;
alert(!!temp);

結(jié)果:false

3.對空字符串的"非"返回true

var temp="";
alert(temp);

結(jié)果:空

var temp="";
alert(!temp);

結(jié)果:true

var temp="";
alert(!!temp);

結(jié)果:false

4.對非零整型的"非"返回false

var temp=1;
alert(temp);

結(jié)果:1

var temp=1;
alert(!temp);

結(jié)果:false

var temp=1;
alert(!!temp);

結(jié)果:true

5.對0的"非"返回true

var temp = 0;
alert(temp);

結(jié)果:0

var temp = 0;
alert(!temp);

結(jié)果:true

var temp = 0;
alert(!!temp);

結(jié)果:false

6.對字符串的"非"返回false

var temp="ab";
alert(temp);

結(jié)果:ab

var temp="ab";
alert(!temp);

結(jié)果:false

var temp="ab";
alert(!!temp);

結(jié)果:true

7.對數(shù)組的"非"返回false

var temp=[1,2];
alert(temp);

結(jié)果:1,2

var temp=[1,2];
alert(!temp);

結(jié)果:false

var temp=[1,2];
alert(!!temp);

結(jié)果:true

相信本文所述對大家的javascript程序設(shè)計的學(xué)習(xí)有一定的借鑒價值。

相關(guān)文章

最新評論