DIV CSS 佈局教程網

JavaScript 六大資料型別完全解析:從 Undefined 到 Object
編輯:關於JavaScript     

先看清 ECMAScript 的型別版圖

JavaScript(嚴格來說是 ECMAScript)把型別分成兩大類:五種簡單型別,也就是我們常說的基本型別——Undefined、Null、Boolean、Number、String;再加上唯一的一種複雜型別——Object。這六個名字記牢,後面的型別轉換、相等比較與真假判斷才有依據可循。

Undefined:宣告了,但還沒賦值

用 var 宣告了一個變數卻沒有給它初始值,這個變數的值就是 undefined。

var number;
document.write(number); //undefined

但如果變數從未宣告就直接拿去用,瀏覽器會直接丟出錯誤,兩者意義完全不同。

document.write(str); //錯誤:未定義的識別符

有意思的是,typeof 對這兩種情況都會回傳 "undefined",它不會因為變數未宣告而報錯。

var num;
document.write(typeof num); //undefined
document.write(typeof str); //undefined

Null:刻意留出的空位

用 typeof 檢測 null,得到的是 "object",這是 JavaScript 早期留下的歷史包袱。

var num = null;
document.write(typeof num); //object

undefined 實際上派生自 null,因此兩者用 == 比較會得到 true。

alert(null == undefined); //true

實務上有一個約定:如果某個變數「未來要存物件,但目前還沒有」,就應該明確讓它等於 null,這樣才能和「尚未賦值」的 undefined 區分開來。

var num1 = null;
var num2;
document.write(typeof num1 + " " + typeof num2); //object undefined

Boolean:用轉型函數 Boolean() 明確轉換

要把任意值轉成對應的布林值,可以呼叫轉型函數 Boolean()。

var str = "helloworld";
document.write(Boolean(str)); //true

《JavaScript 高級編程設計》一書中整理了各型別的轉換規則,可以濃縮成下面幾條:

  • 字串:空字串轉為 false,任何非空字串都轉為 true。
  • 數字:0 與 NaN 轉為 false,其餘(包含負數與 Infinity)都是 true。
  • 物件:除了 null 之外,一律轉為 true。
  • undefined 與 null:都轉為 false。

值得一提的是,if 語句本身就會自動做一次布林轉換,所以通常不需要手動呼叫 Boolean()。

var str = "helloworld";
if (str) {
  document.write("hellothere");
} //hellothere

Number:整數、浮點數與特殊值

除了十進位,整數還可以用八進位或十六進位字面值來寫。八進位字面值第一位必須是 0,後面接 0~7 的數字序列。

var num1 = 070; //八進位的 56
var num2 = 032; //八進位的 26
var num3 = 079; //錯誤:9 超出八進位範圍
var num4 = 09;  //錯誤:同上

十六進位字面值前兩位必須是 0x,後面接 0~9 及 A~F,字母不分大小寫。

var num1 = 0xA; //十進位的 10
var num2 = 0xa; //同樣是 10

浮點數:永遠不要直接比較

由於二進位浮點數的精度限制,浮點運算結果常常帶有小尾巴,因此不要用 === 去比較兩個浮點值。

var a = 0.1;
var b = 0.2;
if (a + b == 0.3) {
  document.write("you are right");
}
//不會執行,因為 0.1 + 0.2 實際上等於 0.30000000000000004

數值範圍與 isFinite()

ECMAScript 能表達的最大與最小數值分別保存在 Number.MAX_VALUE 與 Number.MIN_VALUE 中。要判斷某個值是否有限,可以使用 isFinite()。

document.write(isFinite(Number.MAX_VALUE + Number.MAX_VALUE)); //false

NaN:不是數字的那個「數字」

0 除以 0 得到 NaN,正數除以 0 得到 Infinity,負數除以 0 則是 -Infinity。NaN 有個很特殊的性質:它跟任何值都不相等,連它自己也不例外。要判斷某個值「是不是非數字」,請用 isNaN()。

document.write(isNaN("a"));   //true
document.write(isNaN("324")); //false
document.write(isNaN(true));  //false
document.write(isNaN(false)); //false
document.write(isNaN(NaN));   //true

數值轉換:Number()、parseInt()、parseFloat()

Number() 的轉換規則可以整理如下:布林值 true / false 轉為 1 / 0;數字原樣返回;null 轉為 0;undefined 轉為 NaN。字串的情況則是:

  • 純數字字串轉為十進位數值。
  • 浮點格式字串轉為對應浮點數,前導零會被忽略。
  • 十六進位格式字串會被轉成十進位數。
  • 空字串轉為 0。
  • 其他一律轉為 NaN。
document.write(Number(true));       //1
document.write(Number(false));      //0
document.write(Number("789"));      //789
document.write(Number(null));       //0
document.write(Number(undefined));  //NaN
document.write(Number("02.0942"));  //2.0942
document.write(Number(0xa));        //10
document.write(Number(""));         //0
document.write(Number("fdsa"));     //NaN

parseInt():從頭解析,遇到非數字就停

  • 第一個字元不是數字或正負號時,回傳 NaN。
  • 空字串回傳 NaN。
  • 第一個字元是數字時,會往後逐字解析,直到碰到非數字字元為止。
document.write(parseInt("fds"));         //NaN
document.write(parseInt(""));            //NaN
document.write(parseInt("1111112abc"));  //1111112
document.write(parseInt("-1111112abc")); //-1111112
document.write(parseInt("+1111112abc")); //1111112
document.write(parseInt("0xa"));         //10
document.write(parseInt("0022.00009"));  //22
document.write(parseInt("070"));         //ES3 視為 56(八進位),ES5 視為 70(十進位)

ES5 之後已經不再自動解析八進位,為了避免版本差異帶來的歧義,建議永遠傳入第二個參數——基數:

document.write(parseInt("070", 10)); //70
document.write(parseInt("070", 8));  //56
document.write(parseInt("070", 16)); //112

多數情況下,把基數固定寫成 10 是最安全的做法。

parseFloat():只認十進位小數

  • 行為與 parseInt() 類似,但第一個小數點是有效的,第二個小數點開始(含)即失效。
  • 它無法解析十六進位數值。
  • 它只解析十進位數值,而且沒有第二個基數參數可用。
document.write(parseFloat("421"));      //421
document.write(parseFloat("0421.32.1"));//421.32
document.write(parseFloat("0xaafd"));   //0
document.write(parseFloat("070"));      //70
document.write(parseFloat("070abc"));   //70
document.write(parseFloat(""));         //NaN
document.write(parseFloat("abc"));      //NaN

String:toString() 與 String()

把值轉成字串有兩條路。第一條是幾乎所有值都具備的 toString() 方法,它還可以帶入進位參數。

document.write((533).toString(10));  //"533"
document.write((0xa).toString(10));  //"10"
document.write((0xa).toString(2));   //"1010"
document.write((true).toString(10)); //"true"
document.write((false).toString(10));//"false"

但 null 與 undefined 沒有 toString(),直接呼叫會出錯。

document.write((null).toString(10));      //報錯
document.write((undefined).toString(10)); //報錯

如果手上拿到的值可能不是 null 就是 undefined,改用轉型函數 String() 更保險:null 會得到 "null",undefined 會得到 "undefined"。

document.write(String(null));      //"null"
document.write(String(undefined)); //"undefined"

至於 Object 型別的細節,留待下一篇再深入。以上就是 JavaScript 資料型別的入門整理,希望對你的學習有幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved