| 方法 | 基本型別 | 引用型別 | null/undefined | 備註 |
|---|---|---|---|---|
typeof | ✅ 精確 | ❌ 全是 "object" | null→"object" undefined→"undefined" | 最快、最常用 |
instanceof | ❌ 報錯 | ✅ 原型鏈判斷 | ❌ 報錯 | 檢測建構函數 prototype |
constructor | ✅ (包裝物件) | ✅ 但可被篡改 | ❌ 無屬性 | 指向建構函數 |
Object.prototype.toString.call() | ✅ 精確 | ✅ 精確 [object Type] | ✅ 精確 | 最可靠、通用 |
typeof true // "boolean"
typeof 123 // "number"
typeof "str" // "string"
typeof Symbol() // "symbol"
typeof undefined // "undefined"
typeof null // "object" ← 著名 Bug
typeof {} // "object"
typeof [] // "object"
typeof function(){} // "function"
typeof new Date() // "object"
適用:基本型別 + function 判斷。
[] instanceof Array // true
[] instanceof Object // true (原型鏈向上)
new Date() instanceof Date // true
new Date() instanceof Object // true
function(){} instanceof Function // true
// 陷阱:跨 iframe/window 原型鏈斷裂
const iframe = document.createElement("iframe");
document.body.appendChild(iframe);
const iArray = iframe.contentWindow.Array;
[] instanceof iArray // false!不同全域環境
適用:同一全域環境下判斷具體建構函數(Array、Date、自定義類)。
(123).constructor === Number // true (自動包裝)
"str".constructor === String // true
[].constructor === Array // true
({}).constructor === Object // true
new Date().constructor === Date // true
// 陷阱:可被修改
function Foo() {}
const f = new Foo();
f.constructor = Bar; // 竄改
f.constructor === Foo // false
適用:快速獲取建構函數名稱(obj.constructor.name),但不可信任安全性。
const toString = Object.prototype.toString;
toString.call(123) // "[object Number]"
toString.call("str") // "[object String]"
toString.call(true) // "[object Boolean]"
toString.call(undefined) // "[object Undefined]"
toString.call(null) // "[object Null]"
toString.call({}) // "[object Object]"
toString.call([]) // "[object Array]"
toString.call(function(){}) // "[object Function]"
toString.call(new Date()) // "[object Date]"
toString.call(/regex/) // "[object RegExp]"
toString.call(new Error()) // "[object Error]"
toString.call(Math) // "[object Math]"
toString.call(JSON) // "[object JSON]"
toString.call(arguments) // "[object Arguments]"
toString.call(document) // "[object HTMLDocument]"
toString.call(new Map()) // "[object Map]"
toString.call(new Set()) // "[object Set]"
toString.call(new Promise()) // "[object Promise]"
function typeOf(val) {
if (val === null) return "null";
if (val === undefined) return "undefined";
const type = typeof val;
if (type !== "object") return type;
// 引用型別:用 toString 精確判斷
return toString.call(val).slice(8, -1).toLowerCase(); // "array", "date", ...
}
// 測試
typeOf(null) // "null"
typeOf([]) // "array"
typeOf(new Date()) // "date"
typeOf(/re/) // "regexp"
typeOf(async function(){}) // "function" (async function 仍是 function)
Array.isArray([]) // true
Array.isArray(new Array()) // true
Array.isArray({}) // false
// 跨 iframe 也正常工作
function isPlainObject(obj) {
if (!obj || toString.call(obj) !== "[object Object]") return false;
const proto = Object.getPrototypeOf(obj);
return proto === null || proto === Object.prototype;
}
isPlainObject({}) // true
isPlainObject(Object.create(null)) // true (無原型)
isPlainObject(new Foo()) // false
isPlainObject([]) // false
function isThenable(val) {
return val && typeof val.then === "function";
}
typeof=== 比較Array.isArray()Object.prototype.toString.call()instanceof(同一全域環境)typeOf() 統一入口