welcome to Cheeto's blog

0%

JavaScript核心篇:原始型別、物件型別

原始型別、物件型別

JavaScript 主要分為 原始型別、物件型別 兩大類。

原始型別

  • Boolean 布林
  • Null 空值
  • Undefined 未定義
  • Number 數值
  • String 字串

ES6

  • Bight 整體數值
  • Symbol
1
2
3
4
5
6
7
8
9
10
11
12
13
var a, b, c, d;
console.log(typeof a); // undefined
a = 1;
console.log(typeof a); // number
a = '文字';
console.log(typeof a); // string
b = true;
console.log(typeof b); // boolean
c = {};
console.log(typeof c); // object
d = null; // 音標 nʌl
console.log(d , typeof d); // object
console.log(typeof e); // undefined
  • null 會被 typeof 判定成是 object 是 JavaScript 長久以來的 bug。
  • 正常來說沒有宣告 e 為變數 console.log(e) 的話會出現 not defined,但是 not defined 在 typeof 裡面會把它轉換成 undefined,這是 typeof 針對 not defined 的一個保護措施。

包裹物件

基本型別包裹器

1
2
3
4
var a = 'ming';
console.log(a);
var e = new String(a);
console.log(e);

包裹物件

這邊要注意的是 e 經過 new String() 的轉化已經變成一個物件了。而包裹物件的功能可以在 proto 裡面看到。