1. 程式人生 > 實用技巧 >JavaScript 07 型別轉換

JavaScript 07 型別轉換

示例1:

偽物件

偽物件概念:javascript是一門很有意思的語言,即便是基本型別,也是偽物件,所以他們都有屬性和方法。
變數a的型別是字串,通過呼叫其為偽物件的屬性length獲取其長度

<script>
  var a="hello javascript";
  document.write("變數a的型別是:"+(typeof a));
  document.write("<br>");
  document.write("變數a的長度是:"+a.length);
</script>

示例2:

轉換為字串

無論是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>

示例3:

數字轉字串

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>

示例4:

轉換為數字

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>

示例5:

轉換為Boolean

使用內建函式Boolean() 轉換為Boolean值
當轉換字串時:
非空即為true
當轉換數字時:
非0即為true
當轉換物件時:
非null即為true

<script>
  document.write("空字串''轉換為布林後的值:"+Boolean(""));  //空字串
  document.write("<br>");
  document.write("非空字元'hello javascript '串轉換為布林後的值:"+Boolean("hello javascript"));  //非空字串
  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 轉換為布林後的值:"+Boolean(null));  //null
  document.write("<br>");
  document.write("非空物件 new Object() 轉換為布林後的值:"+Boolean(new Object()));  //物件存在
  document.write("<br>");
 
</script>

示例6:

Number()和parseInt()的區別

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>

示例7:

String()和toString()的區別

String()和toString()一樣都會返回字串,區別在於對null的處理
String()會返回字串"null"
toString() 就會報錯,無法執行

1 <script>
2   var a = null;
3   document.write('String(null) 把空物件轉換為字串:'+String(a));  
4   document.write("<br>");  
5   document.write('null.toString() 就會報錯,所以後面的程式碼不能執行hhhhh');  
6   document.write(a.toString());  
7   document.write("因為第6行報錯,所以這一段文字不會顯示");  
8 </script>