在 JavaScript 中判斷資料型別有幾種常見做法,各自的能力與限制都不太一樣。下面整理四種主流方式,並附上實作範例,方便你依情境選用最合適的一種。
【輸出】首字母小寫的字串形式
【功能】
[a] 能辨識標準型別(但會把 Null 誤判為 object)
[b] 無法辨識具體的物件型別(Function 除外)
console.log(typeof "jerry");//"string" console.log(typeof 12);//"number" console.log(typeof true);//"boolean" console.log(typeof undefined);//"undefined" console.log(typeof null);//"object" console.log(typeof {name: "jerry"});//"object" console.log(typeof function(){});//"function" console.log(typeof []);//"object" console.log(typeof new Date);//"object" console.log(typeof /\d/);//"object" function Person(){}; console.log(typeof new Person);//"object"
【輸出】[object 資料型別] 的字串形式
【功能】
[a] 能辨識標準型別及內建物件型別
[b] 無法辨識自訂型別
【構造方法】
function type(obj){ return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase(); }
【實例一】
console.log(Object.prototype.toString.call("jerry"));//[object String] console.log(Object.prototype.toString.call(12));//[object Number] console.log(Object.prototype.toString.call(true));//[object Boolean] console.log(Object.prototype.toString.call(undefined));//[object Undefined] console.log(Object.prototype.toString.call(null));//[object Null] console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object] console.log(Object.prototype.toString.call(function(){}));//[object Function] console.log(Object.prototype.toString.call([]));//[object Array] console.log(Object.prototype.toString.call(new Date));//[object Date] console.log(Object.prototype.toString.call(/\d/));//[object RegExp] function Person(){}; console.log(Object.prototype.toString.call(new Person));//[object Object]
【實例二】
function type(obj){ return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase(); } console.log(type("jerry"));//"string" console.log(type(12));//"number" console.log(type(true));//"boolean" console.log(type(undefined));//"undefined" console.log(type(null));//"null" console.log(type({name: "jerry"}));//"object" console.log(type(function(){}));//"function" console.log(type([]));//"array" console.log(type(new Date));//"date" console.log(type(/\d/));//"regexp" function Person(){}; console.log(type(new Person));//"object"
【輸出】function 資料型別(){[native code]} 或 function 自訂型別(){}
【功能】
[a] 能辨識標準型別、內建物件型別及自訂型別
[b] 無法辨識 undefined、null,會報錯
【構造方法】
function type(obj){ var temp = obj.constructor.toString(); return temp.replace(/^function (\w+)\(\).+$/,'$1'); }
【實例一】
console.log(("jerry").constructor);//function String(){[native code]} console.log((12).constructor);//function Number(){[native code]} console.log((true).constructor);//function Boolean(){[native code]} //console.log((undefined).constructor);//報錯 //console.log((null).constructor);//報錯 console.log(({name: "jerry"}).constructor);//function Object(){[native code]} console.log((function(){}).constructor);//function Function(){[native code]} console.log(([]).constructor);//function Array(){[native code]} console.log((new Date).constructor);//function Date(){[native code]} console.log((/\d/).constructor);//function RegExp(){[native code]} function Person(){}; console.log((new Person).constructor);//function Person(){}
【實例二】
function type(obj){ var temp = obj.constructor.toString().toLowerCase(); return temp.replace(/^function (\w+)\(\).+$/,'$1'); } console.log(type("jerry"));//"string" console.log(type(12));//"number" console.log(type(true));//"boolean" //console.log(type(undefined));//錯誤 //console.log(type(null));//錯誤 console.log(type({name: "jerry"}));//"object" console.log(type(function(){}));//"function" console.log(type([]));//"array" console.log(type(new Date));//"date" console.log(type(/\d/));//"regexp" function Person(){}; console.log(type(new Person));//"person"
【輸出】true 或 false
【功能】
[a] 能辨識內建物件型別、自訂型別及其父型別
[b] 無法辨識標準型別,會回傳 false
[c] 無法辨識 undefined、null,會報錯
console.log("jerry" instanceof String);//false console.log(12 instanceof Number);//false console.log(true instanceof Boolean);//false //console.log(undefined instanceof Undefined);//報錯 //console.log(null instanceof Null);//報錯 console.log({name: "jerry"} instanceof Object);//true console.log(function(){} instanceof Function);//true console.log([] instanceof Array);//true console.log(new Date instanceof Date);//true console.log(/\d/ instanceof RegExp);//true function Person(){}; console.log(new Person instanceof Person);//true console.log(new Person instanceof Object);//true
以上就是 JavaScript 四種型別判斷方式的完整整理。實務上,若只想快速區分基本型別可用 typeof;若要精準辨識各種物件型別,Object.prototype.toString 最為可靠;constructor 與 instanceof 則各有適用場合,搭配使用才能應付各種情境。希望對你有所幫助。