標籤:樣本 nbsp 字元 var 整數 code oat 情況 數字
樣本 1 : 偽對象 樣本 2 : 轉換為字串 樣本 3 : 數字轉字串 樣本 4 : 轉換為數字 樣本 5 : 轉換為Boolean 樣本 6 : Number()和parseInt()的區別 樣本 7 : String()和toString()的區別
偽對象概念:javascript是一門很有意思的語言,即便是基本類型,也是偽對象,所以他們都有屬性和方法。變數a的類型是字串,通過調用其為偽對象的屬性length擷取其長度
<script>
var a="hello javascript";
document.write("變數a的類型是:"+(typeof a));
document.write("<br>");
document.write("變數a的長度是:"+a.length);
</script>
無論是Number,Boolean還是String都有一個toString方法,用於轉換為字串<script> var a=10; document.write("數字 "+a+" 轉換為字串"+a.toString()); document.write("<br>"); var b=true; document.write("布爾 "+b+" 轉換為字串"+b.toString()); document.write("<br>"); var c="hello javascript"; document.write("字串 "+c+" 轉換為字串 "+c.toString()); document.write("<br>"); </script>
Number轉換為字串的時候有預設模式和基模式兩種<script> var a=10; document.write(‘預設模式下,數字10轉換為十進位的‘+a.toString()); //預設模式,即十進位 document.write("<br>"); document.write(‘基模式下,數字10轉換為二進位的‘+a.toString(2)); //基模式,二進位 document.write("<br>"); document.write(‘基模式下,數字10轉換為八進位的‘+a.toString(8)); //基模式,八進位 document.write("<br>"); document.write(‘基模式下,數字10轉換為十六進位的‘+a.toString(16)); //基模式,十六進位 document.write("<br>"); </script>
javascript分別提供內建函數 parseInt()和parseFloat(),轉換為數字註:如果被轉換的字串,同時又數字和字元構成,那麼parseInt會一直定位元字,直到出現非字元。 所以"10abc" 會被轉換為 10<script> document.write("字串的\"10\"轉換為數位:"+parseInt("10")); //轉換整數 document.write("<br>"); document.write("字串的\"3.14\"轉換為數位:"+parseFloat("3.14"));//轉換浮點數 document.write("<br>"); document.write("字串的\"10abc\"轉換為數位:"+parseInt("10abc")); //判斷每一位,直到發現不是數位那一位 document.write("<br>"); document.write("字串的\"hello javascript\"轉換為數位:"+parseInt("hello javascript")); //如果完全不包含數字,則返回NaN - Not a Number document.write("<br>"); </script>
使用內建函數Boolean() 轉換為Boolean值當轉換字串時:非空即為true當轉換數字時:非0即為true當轉換對象時:非null即為true<script> document.write("Null 字元串‘‘轉換為布爾後的值:"+Boolean("")); //Null 字元串 document.write("<br>"); document.write("非Null 字元‘hello javascript ‘串轉換為布爾後的值:"+Boolean("hello javascript")); //非Null 字元串 document.write("<br>"); document.write("數字 0 轉換為布爾後的值:"+Boolean(0)); //0 document.write("<br>"); document.write("數字 3.14 轉換為布爾後的值:"+Boolean(3.14)); //非0 document.write("<br>"); document.write("Null 物件 null 轉換為布爾後的值:"+Boolean(null)); //null document.write("<br>"); document.write("非對象 new Object() 轉換為布爾後的值:"+Boolean(new Object())); //對象存在 document.write("<br>"); </script>
Number()和parseInt()一樣,都可以用來進行數位轉換區別在於,當轉換的內容包含非數位時候,Number() 會返回NaN(Not a Number)parseInt() 要看情況,如果以數字開頭,就會返回開頭的合法數字部分,如果以非數字開頭,則返回NaN<script> document.write("通過Number() 函數轉換字串‘123‘ 後得到的數字:"+Number("123")); //正常的 document.write("<br>"); document.write("通過Number() 函數轉換字串‘123abc‘ 後得到的數字:"+Number("123abc")); //包含非數字 document.write("<br>"); document.write("通過Number() 函數轉換字串‘abc123‘ 後得到的數字:"+Number("abc123")); //包含非數字 document.write("<br>"); document.write("通過parseInt() 函數轉換字串‘123‘ 後得到的數字:"+parseInt("123")); //正常的 document.write("<br>"); document.write("通過parseInt() 函數轉換字串‘123abc‘ 後得到的數字:"+parseInt("123abc")); //包含非數字,返回開頭的合法數字部分 document.write("<br>"); document.write("通過parseInt() 函數轉換字串‘abc123‘ 後得到的數字:"+parseInt("abc123")); //包含非數字,以非數字開頭,返回NaN document.write("<br>"); </script>
String()和toString()一樣都會返回字串,區別在於對null的處理String()會返回字串"null"toString() 就會報錯,無法執行<script> var a = null; document.write(‘String(null) 把Null 物件轉換為字串:‘+String(a)); document.write("<br>"); document.write(‘null.toString() 就會報錯,所以後面的代碼不能執行‘); document.write(a.toString()); document.write("因為第5行報錯,所以這一段文字不會顯示"); </script>
JAVASCRIPT 類型轉換