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

javascript中2個(gè)感嘆號(hào)的用法實(shí)例詳解

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

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

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

一、應(yīng)用場(chǎng)景:判斷一個(gè)對(duì)象是否存在

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

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

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

如果僅僅打印對(duì)象,無(wú)法判斷是否存在:

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

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

如果對(duì)json對(duì)象實(shí)施!或!!,就可以判斷該json對(duì)象是否存在:

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

結(jié)果:false

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

結(jié)果:true

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

1.對(duì)null的"非"返回true

var temp = null;
alert(temp); 

結(jié)果:null

var temp = null;
alert(!temp); 

結(jié)果:true

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

結(jié)果:false

2.對(duì)undefined的"非"返回true

var temp;
alert(temp);

結(jié)果:undefined

var temp;
alert(!temp);

結(jié)果:true

var temp;
alert(!!temp);

結(jié)果:false

3.對(duì)空字符串的"非"返回true

var temp="";
alert(temp);

結(jié)果:空

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

結(jié)果:true

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

結(jié)果:false

4.對(duì)非零整型的"非"返回false

var temp=1;
alert(temp);

結(jié)果:1

var temp=1;
alert(!temp);

結(jié)果:false

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

結(jié)果:true

5.對(duì)0的"非"返回true

var temp = 0;
alert(temp);

結(jié)果:0

var temp = 0;
alert(!temp);

結(jié)果:true

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

結(jié)果:false

6.對(duì)字符串的"非"返回false

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

結(jié)果:ab

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

結(jié)果:false

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

結(jié)果:true

7.對(duì)數(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

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

相關(guān)文章

最新評(píng)論