Javascript的Boolean對象

來源:互聯網
上載者:User

一、瞭解Boolean對象

Boolean對象也成布爾對象,表示邏輯,true和false

二、建立Boolean對象

 1 <script language="javascript"> 2 //Boolean對象也成布爾對象,表示邏輯,true和false 3 /*建立Boolean對象的文法: 4          //建構函式 5          new Boolean(value); 6          //轉換函式 7          Boolean(value); 8          提示:value 參數可以是要轉換成布爾對象的值,也可以是布爾對象存放的值。 9 */10 document.write(typeof(new Boolean(1))+"<br />");//利用建構函式(new 關鍵字)得到的是一個包含布爾值的對象  輸出:object11 document.write(typeof(Boolean(1))+"<br />");//使用轉換函式,得到的則是一個布爾值。  輸出:boolean12 13 //------------------------注意一-------------------------14 //如果 value 參數被設定為 0、-0、null、""、false、undefined 或 NaN,或者省略該參數,[該 Boolean 對象設定為 false],反之則為 true。15 document.write(new Boolean(0)+"<br />");//false16 document.write(new Boolean(-0)+"<br />");//false後面都是17 document.write(new Boolean(null)+"<br />");18 document.write(new Boolean("")+"<br />");19 document.write(new Boolean(false)+"<br />");20 document.write(new Boolean(undefined)+"<br />");21 document.write(new Boolean(NaN)+"<br />");22 document.write(new Boolean()+"<br />");23 24 //------------------------注意二-------------------------25 //儘管 Boolean 對象被設定為 false ,但在 if 判斷中仍為 true:26 var boo=new Boolean(false);//將Boolean對象設定為false27 if(boo){document.write(1);}28 else {document.write(2);}29 //結果輸出:130 //解析:原因在於儘管 boo 是一個被設定為 false 的對象,但在 if 判斷時,boo 是一個非空的對象,因此認為它是有意義的,返回 true 而判斷成立。這與 obj1 對象包含的布爾值無關。31 </script>

三、Boolean屬性|Boolean方法

Boolean 對象支援 prototype 屬性來增加或重新定義對象的屬性和方法,詳細參見《JavaScript 對象的 prototype 屬性和方法》。---5idev.com

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.