javascript / js數據類型,數據類型轉換

 

1)基本類型
—數字,采用IEEE754標準定義的64位浮點格式.
特殊數值常量:
Infinity 無窮大的特殊值
NaN 非數字值
Number.MAX_VALUE 可表示的最大數字
Number.MIN_VALUE 可表示的最小數字
Number.NaN 非數字值
Number.POSITIVE_INFINITY 正無窮大
Number.NEGATIVE_INFINITY 負無窮大
把數字轉為字符串6種方式
var n = 1.23456;
var n_as_str = n+"";
String(n);
n.toString(x); //x=2,binary; x=8, octonay; x=16,hexadecimal.if empty,decimal
n.toFixed(x); //小數點後位數
n.toExponential(x); //顯示指數形式,x表示小數位
n.toPrecision(x); //若n位數>x時顯示為指數,x表示數字的精度
—字符串
字符串轉為數字
在數字環境,自動轉換為數字,
var num = "2" * "3"; //num = 6
var num = str_val – 0;
var num = Number(str_val); //以10為基數的數字有效,允許開頭和結尾的空白
parseInt(str)
parseInt(str,radix) //the same with java
parseFloat(str)
—佈爾
顯式轉換的方法
var x_as_boolean = Boolean(x);
var x_as_boolean = !!x;
—null
表示"無值".
對象轉換:佈爾環境式時,非空對象為false;字符串環境時"null";數字環境時0;
—undefined
使用未聲明的變量時,或使用聲明但沒有賦值的變量時,或使用不存在的對象屬性時,返回
undefined.
對象轉換:佈爾環境式時,非空對象為false;字符串環境時"undefined";數字環境時NaN;
與null區別:
null是關鍵字,undefined不是.(ECMAScript v3定義瞭undefined的全局變量,初始值是undefined)
 
<html>
  <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title>null and undefined</title>
  </head>

  <body>
 <p>比較null與undefined</p>

 <script>
   var undef;
   document.write("佈爾環境: ")
   document.write(undef==null);      //true
   document.write("<br/>");
   document.write("字苻串環境: ")
   document.write("".undef);         //undefined
   document.write("<br/>");
   document.write("數字環境: ")
   document.write(1+undef);          //NaN
   document.write("<br/>");

   document.write("undef===null: ")
   document.writeln(undef===null);     //false
   document.write("<br/>");
   document.write("typeof undef: ")
   document.writeln(typeof undef);     //undefined
 </script>

  </body>
</html>
 
2)復合類型
對象:已命名的數據的集合
對象直接量:由一個列表構成.列表的表式形式,{key:value,*};(key=標識符/字符串,value=常量/表達式)
對象轉換:佈爾環境式時,非空對象為true;字符串環境時,toString();數字環境時,valueOf();
數組
不直持多維數組,數組元素可以是數組;
數組元素不必據有相同的類型
 
3)特殊對象
函數
一般語法,function func_name(args) {func_body;}
lambda函數,function(args){func_body;}
構造函數,new Function("args","func_body");
 
*說明
計劃以後在note目錄下發佈些整理的筆記,好記心不如爛筆頭.主要為瞭方便自己查找,若讀者看瞭覺得哪兒理解不對,請指教.
這篇是關於javascript的數據類型,主要內容來自"javascript權威指南".

 摘自 hnyei-WEB前端設計經驗
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *