詳解JavaScript類(lèi)型判斷的四種方法
JavaScript有八種內(nèi)置類(lèi)型,除對(duì)象外,其他統(tǒng)稱(chēng)為“基本類(lèi)型”。
- 空值(null)
- 未定義(undefined)
- 布爾值(boolean)
- 數(shù)字(number)
- 字符串(string)
- 對(duì)象 (object)
- 符號(hào)(symbol, ES6中新增)
- 大整數(shù)(BigInt, ES2020 引入)
Symbol: 是ES6中引入的一種原始數(shù)據(jù)類(lèi)型,表示獨(dú)一無(wú)二的值。
BigInt:是 ES2020 引入的一種新的數(shù)據(jù)類(lèi)型,用來(lái)解決 JavaScript中數(shù)字只能到 53 個(gè)二進(jìn)制位(JavaScript 所有數(shù)字都保存成 64 位浮點(diǎn)數(shù),大于這個(gè)范圍的整數(shù),無(wú)法精確表示的問(wèn)題。具體可查看:新數(shù)據(jù)類(lèi)型 — BigInt
一、typeof
typeof是一個(gè)操作符而不是函數(shù),其右側(cè)跟一個(gè)一元表達(dá)式,并返回這個(gè)表達(dá)式的數(shù)據(jù)類(lèi)型。返回的結(jié)果用該類(lèi)型的字符串(全小寫(xiě)字母)形式表示,包括以下 8 種:number、boolean、symbol、string、object、undefined、function 、bigInt等。
typeof原理是不同的對(duì)象在底層都表示為二進(jìn)制,在Javascript中二進(jìn)制前(低)三位存儲(chǔ)其類(lèi)型信息。
- 000: 對(duì)象
- 010: 浮點(diǎn)數(shù)
- 100:字符串
- 110:布爾
- 1:整數(shù)
console.log(typeof undefined) // undefind
console.log(typeof null) // object
console.log(typeof true) // boolean
console.log(typeof 43) // number
console.log(typeof '21') // string
console.log(typeof {a:1}) // object
console.log(typeof Symbol()) // symbol
console.log(typeof 123n) // bigint
function a() {}
console.log(typeof a) // function
var date = new Date()
var error = new Error()
console.log(typeof date) // object
console.log(typeof error) // object
二、instanceof
instanceof 是用來(lái)判斷 A 是否為 B 的實(shí)例,表達(dá)式為:A instanceof B,如果 A 是 B 的實(shí)例,則返回 true,否則返回 false。 在這里需要特別注意的是:instanceof 檢測(cè)的是原型
通俗一些講,instanceof 用來(lái)比較一個(gè)對(duì)象是否為某一個(gè)構(gòu)造函數(shù)的實(shí)例。注意,instanceof可以準(zhǔn)確的判斷復(fù)雜數(shù)據(jù)類(lèi)型,但是不能正確判斷基本數(shù)據(jù)類(lèi)型
console.log(12 instanceof Number) // false
console.log('22' instanceof String) // false
console.log(true instanceof Boolean) // false
console.log(null instanceof Object) // false
console.log(undefined instanceof Object) // false
console.log([] instanceof Array) // true
console.log({a: 1} instanceof Object) // true
console.log(json instanceof Object) // true
function a() {}
console.log(a instanceof Function) // true
console.log(new Date() instanceof Date) //true
console.log(reg instanceof RegExp) //true
console.log(error instanceof Error) // true
三、Object.prototype.toString.call()
toString() 是 Object 的原型方法,調(diào)用該方法,默認(rèn)返回當(dāng)前對(duì)象的 [[Class]] 。這是一個(gè)內(nèi)部屬性,其格式為 [object Xxx] ,其中 Xxx 就是對(duì)象的類(lèi)型。
對(duì)于 Object 對(duì)象,直接調(diào)用 toString() 就能返回 [object Object] 。而對(duì)于其他對(duì)象,則需要通過(guò) call / apply 來(lái)調(diào)用才能返回正確的類(lèi)型信息。
console.log(Object.prototype.toString.call(1)) // [object Number]
console.log(Object.prototype.toString.call(1n)) // [object BigInt]
console.log(Object.prototype.toString.call('123')) // [object String]
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({})) // [object Object]
console.log(Object.prototype.toString.call([])) // [object Array]
console.log(Object.prototype.toString.call(function a() {})) // [object Function]
console.log(Object.prototype.toString.call(Symbol())) // [object Symbol]
console.log(Object.prototype.toString.call(Math)) // [object Math]
console.log(Object.prototype.toString.call(JSON)) // [object JSON]
console.log(Object.prototype.toString.call(new Date())) // [object Date]
console.log(Object.prototype.toString.call(new RegExp())) // [object RegExp]
console.log(Object.prototype.toString.call(new Error)) // [object Error]
console.log(Object.prototype.toString.call(window) // [object Window]
console.log(Object.prototype.toString.call(document) // [object HTMLDocument]
使用該方法我們可以封裝一個(gè)isType方法來(lái)對(duì)類(lèi)型進(jìn)行判斷
let isType = (type, obj) => {
return Object.prototype.toString.call(obj) === `[object ${type}]`
}
console.log(isType('Number', 12)) // true
console.log(isType('Number', '12')) // false
或者
let type = function(o) {
let s = Object.prototype.toString.call(o);
return s.match(/\[object (.*?)\]/)[1].toLowerCase();
};
console.log(type(12)) // number
console.log(type('12')) // string
console.log(type({})) // object
console.log(type([])) // array
四、constructor
constructor屬性,可以得知某個(gè)實(shí)例對(duì)象,到底是哪一個(gè)構(gòu)造函數(shù)產(chǎn)生的。
constructor屬性表示原型對(duì)象與構(gòu)造函數(shù)之間的關(guān)聯(lián)關(guān)系,如果修改了原型對(duì)象,一般會(huì)同時(shí)修改constructor屬性,防止引用的時(shí)候出錯(cuò)。所以,修改原型對(duì)象時(shí),一般要同時(shí)修改constructor屬性的指向。
console.log('22'.constructor === String) // true
console.log(true.constructor === Boolean) // true
console.log([].constructor === Array) // true
console.log(document.constructor === HTMLDocument) // true
console.log(window.constructor === Window) // true
console.log(new Number(22).constructor === Number) // true
console.log(new Function().constructor === Function) // true
console.log((new Date()).constructor === Date) // true
console.log(new RegExp().constructor === RegExp) // true
console.log(new Error().constructor === Error) // true
注意:
1、null 和 undefined 是無(wú)效的對(duì)象,因此是不會(huì)有 constructor 存在的,這兩種類(lèi)型的數(shù)據(jù)需要通過(guò)其他方式來(lái)判斷。
2、函數(shù)的 constructor 是不穩(wěn)定的,這個(gè)主要體現(xiàn)在自定義對(duì)象上,當(dāng)開(kāi)發(fā)者重寫(xiě) prototype 后,原有的 constructor 引用會(huì)丟失,constructor 會(huì)默認(rèn)為 Object
以上就是JavaScript類(lèi)型判斷的四種方法的詳細(xì)內(nèi)容,更多關(guān)于JavaScript類(lèi)型判斷的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
js下拉選擇框與輸入框聯(lián)動(dòng)實(shí)現(xiàn)添加選中值到輸入框的方法
這篇文章主要介紹了js下拉選擇框與輸入框聯(lián)動(dòng)實(shí)現(xiàn)添加選中值到輸入框的方法,涉及javascript中onchange事件及頁(yè)面元素遍歷的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
JavaScript學(xué)習(xí)筆記之?dāng)?shù)組基本操作示例
這篇文章主要介紹了JavaScrip學(xué)習(xí)筆記之?dāng)?shù)組基本操作,結(jié)合實(shí)例形式分析了javascript數(shù)組的基本定義、添加、刪除、修改、連接、排序等操作技巧,需要的朋友可以參考下2019-01-01
詳解JavaScript如何實(shí)現(xiàn)更短時(shí)間的延時(shí)函數(shù)
在項(xiàng)目開(kāi)發(fā)中,經(jīng)常能遇到需要延時(shí)執(zhí)行的需求,比如實(shí)現(xiàn)一個(gè)定時(shí)器功能,本文主要和大家介紹了JS如何實(shí)現(xiàn)更短時(shí)間的延時(shí)函數(shù),需要的可以參考下2024-03-03
JavaScript 手動(dòng)實(shí)現(xiàn)instanceof的方法
instanceof運(yùn)算符用于檢測(cè)構(gòu)造函數(shù)的prototype屬性是否出現(xiàn)在某個(gè)實(shí)例對(duì)象的原型鏈上,本文重點(diǎn)給大家介紹JavaScript手動(dòng)實(shí)現(xiàn)instanceof的問(wèn)題,感興趣的朋友跟隨小編一起看看吧2021-10-10
JS簡(jiǎn)單實(shí)現(xiàn)移動(dòng)端日歷功能示例
這篇文章主要介紹了JS簡(jiǎn)單實(shí)現(xiàn)移動(dòng)端日歷功能的方法,涉及javascript針對(duì)日期與時(shí)間的操作及顯示相關(guān)技巧,需要的朋友可以參考下2016-12-12
JavaScript利用正則表達(dá)式來(lái)禁止鍵盤(pán)輸入數(shù)字
本文主要介紹了JavaScript利用正則表達(dá)式來(lái)禁止鍵盤(pán)輸入數(shù)字,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
layui復(fù)選框限制選擇個(gè)數(shù)的方法
今天小編就為大家分享一篇layui復(fù)選框限制選擇個(gè)數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09

