javascript四種類型識別的方法
前面的話
javascript有複雜的型別系統,型別識別則是基本的功能。javascript總共提供了四種類型識別的方法,本文將對這四種方法進行詳細說明
typeof運算子
typeof是一元運算子,放在單個運算元的前面,返回值為表示運算元型別的首字母小寫的字串
[注意]typeof運算子後面帶不帶圓括號都可以
console.log(typeof 'a');//'string' console.log(typeof ('a'));//'string'
識別
【1】可以識別標準型別(將Null識別為'object')
【2】不能識別具體的物件型別(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"
[注意]判斷一個值是否為null型別的最佳方法是直接和null進行恆等比較
console.log(typeof null);//'object' console.log(null === null);//true console.log(undefined === null);//falseconsole.log('null' === null);//false
instanceof運算子
instanceof是一個二元運算子,左運算元是一個物件,右運算元是一個建構函式。如果左側的物件是右側建構函式的例項物件,則表示式返回true;否則返回false
如果左運算元不是物件,返回false,如果右運算元不是函式,則丟擲一個型別錯誤異常TypeError
console.log(123 instanceof function(){});//false //Uncaught TypeError: Right-hand side of 'instanceof' is not an object console.log({} instanceof 123);
[注意]所有的物件都是Object的例項
識別
【1】可以識別內建物件型別、自定義型別及其父型別
【2】不能識別標準型別,會返回false
【3】不能識別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
constructor屬性
例項物件的constructor屬性指向其建構函式。如果是內建型別,則輸出function 資料型別(){[native code]};如果是自定義型別,則輸出function 資料型別(){}
識別
【1】可以識別標準型別、內建物件型別及自定義型別
【2】不能識別undefined、null,會報錯,因為它倆沒有建構函式
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(){}
可以將constructor屬性封裝成一個型別識別方法
function type(obj){ var temp = obj.constructor.toString(); return temp.replace(/^function (\w+)\(\).+$/,'$1'); }
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"
Object.prototype.toString()方法
物件的類屬性是一個字串,用以表示物件的型別資訊。javascript沒有提供設定這個屬性的方法,但有一種間接方法可以查詢它。Object.prototype.toString()方法返回瞭如下格式的字串:[object 資料型別]
識別
【1】可以識別標準型別及內建物件型別
【2】不能識別自定義型別
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]
可以將Object.prototype.toString()方法封裝成一個型別識別方法
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"
[注意]如果是包裝物件,Object.prototype.toString()方法將返回其原始型別
console.log(Object.prototype.toString.call(new Number(123)));//[object Number] console.log(Object.prototype.toString.call(123));//[object Number] console.log(Object.prototype.toString.call(new String('abc')));//[object String] console.log(Object.prototype.toString.call('abc'));//[object String] console.log(Object.prototype.toString.call(new Boolean(true)));//[object Boolean] console.log(Object.prototype.toString.call(true));//[object Boolean]