詳解JavaScript中的4種類型識別方法
具體內容如下:
1.typeof
【輸出】首字母小寫的字符串形式
【功能】
[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"
2.Object.prototype.toString
【輸出】[object 數(shù)據(jù)類型]的字符串形式
【功能】
[a]可以識別標準類型及內置對象類型
[b]不能識別自定義類型
【構造方法】
function type(obj){
return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();
}
【實例1】
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]
【實例2】
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"
3.constructor
【輸出】function 數(shù)據(jù)類型(){[native code]}或者function 自定義類型(){}
【功能】
[a]可以識別標準類型、內置對象類型及自定義類型
[b]不能識別undefined、null,會報錯
【構造方法】
function type(obj){
var temp = obj.constructor.toString();
return temp.replace(/^function (\w+)\(\).+$/,'$1');
}
【實例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(){}
【實例2】
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"
4.instanceof
【輸出】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中的4種類型識別方法,希望大家喜歡。
相關文章
BootStrap實現(xiàn)帶有增刪改查功能的表格(DEMO詳解)
這篇文章主要介紹了BootStrap實現(xiàn)帶有增刪改查功能的表格,表格封裝了3個版本,接下來通過本文給大家展示下樣式及代碼,對bootstrap增刪改查相關知識感興趣的朋友一起通過本文學習吧2016-10-10
JavaScript中判斷原生函數(shù)檢查function是否是原生代碼
檢查某個function是否是原生代碼,要檢測這一點,最簡單的辦法當然是判斷函數(shù)的 toString 方法返回的值2014-09-09
layui實現(xiàn)數(shù)據(jù)分頁功能(ajax異步)
這篇文章主要為大家詳細介紹了layui實現(xiàn)數(shù)據(jù)分頁功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
傳參安全處理window.btoa base64加密,線性對稱加密
這篇文章主要介紹了傳參安全處理window.btoa base64加密,線性對稱加密,需要的朋友可以參考下2023-07-07

