js數(shù)據(jù)類型檢測(cè)總結(jié)
在js中,有四種用于檢測(cè)數(shù)據(jù)類型的方式,分別是:
- typeof 用來檢測(cè)數(shù)據(jù)類型的運(yùn)算符
- instanceof 檢測(cè)一個(gè)實(shí)例是否屬于某個(gè)類
- constructor 構(gòu)造函數(shù)
- Object.prototype.toString.call() 原型鏈上的Object對(duì)象的toString方法
下面我們就來分別介紹一下上面四種方法的適用場(chǎng)景和局限性。
typeof 用來檢測(cè)數(shù)據(jù)類型的運(yùn)算符
使用typeof檢測(cè)數(shù)據(jù)類型,返回值是字符串格式。能夠返回的數(shù)據(jù)類型
是:"number","string","bolean","undefined","function","object"。
<script> console.log(typeof(1)); //number console.log(typeof('hello')); //string console.log(typeof(true)); //boolean console.log(typeof(undefined)); //undefined console.log(typeof(null)); //object console.log(typeof({})); //object console.log(typeof(function() {})); //function </script>
局限性:
- typeof (null); //"object"。這是由于在js中,null值表示一個(gè)空對(duì)象指針,而這也正是使用typeof操作 符檢測(cè)null值時(shí)會(huì)返回"object"的原因。
- 不能具體的細(xì)分是數(shù)組還是正則,還是對(duì)象中其他的值,因?yàn)槭褂胻ypeof檢測(cè)數(shù)據(jù)類型,對(duì)于對(duì)象數(shù)據(jù)類型的所有的值,最后返回的都是"object"
instanceof 檢測(cè)某一個(gè)實(shí)例是否屬于某個(gè)類
instanceof主要用來彌補(bǔ)typeof不能檢測(cè)具體屬于哪個(gè)對(duì)象的局限性。
<script> let arr = [1,2,3]; let reg = /\w/; console.log(arr instanceof Array); //true console.log(arr instanceof Object); //true console.log(reg instanceof RegExp); //true console.log(reg instanceof Object); //true </script>
局限性:
- 不能用于檢測(cè)和處理字面量方式創(chuàng)建出來的基本數(shù)據(jù)類型值,即原始數(shù)據(jù)類型
- instanceof的特性:只要在當(dāng)前實(shí)例的原型鏈上的對(duì)象,我們用其檢測(cè)出來都為true
- 在類的原型繼承中,我們最后檢測(cè)出來的結(jié)果未必正確
constructor 構(gòu)造函數(shù)
是函數(shù)原型上的屬性,該屬性指向的是構(gòu)造函數(shù)本身。
作用和instsnceof非常相似,與instanceof不同的是,不僅可以處理引用數(shù)據(jù)類型,還可以處理原始數(shù)據(jù)類型。
<script> let num = 12; let obj = {}; console.log(num.constructor == Number);//true console.log(obj.constructor == Object);//true </script>
但是要注意一點(diǎn)的是,當(dāng)直接用(對(duì)象字面量或原始數(shù)據(jù)).constructor時(shí),最好加上()。為了便于理解,我們來看一個(gè)例子。
<script> 1.constructor === Number; //報(bào)錯(cuò),Invalid or unexceped token (1).constructor === Number; //true {}.constructor === Number; //報(bào)錯(cuò),Invalid or unexceped token ({}).constructor === Number; //true </script>
這主要是由于js內(nèi)部解析方式造成的,js會(huì)把1.constructor解析成小數(shù),這顯然是不合理的,小數(shù)點(diǎn)后應(yīng)該是數(shù)字,因此就會(huì)引發(fā)報(bào)錯(cuò)。js會(huì)把{}解析成語句塊來執(zhí)行,這時(shí)后面出現(xiàn)一個(gè)小數(shù)點(diǎn)顯然也是不合理的,因此也會(huì)報(bào)錯(cuò)。為了解決這個(gè)問題,我們可以為表達(dá)式加上()使js能夠正確解析。
局限性:我們可以把類的原型進(jìn)行重寫,在重寫的過程中很可能把之前constructor給覆蓋了,這樣檢測(cè)出來的結(jié)果就是不準(zhǔn)確的
<script> function Fn() {}; Fn.prototype = new Array; var f = new Fn; //f是一個(gè)函數(shù),按道理說他的構(gòu)造函數(shù)應(yīng)該是Function,但是修改其原型鏈后,它的constructor變成了Array. console.log(f.constructor == Array); //true </script>
Object.prototype.toString.call() 原型鏈上的Object對(duì)象的toString方法
Object.prototype.toString的作用是返回當(dāng)前方法的執(zhí)行主體(方法中的this)所屬類的詳細(xì)信息,是最全面也是最常用的檢測(cè)數(shù)據(jù)類型的方式。
返回值的類型為string類型。
<script> console.log(Object.prototype.toString.call(1)); //[object Number] console.log(Object.prototype.toString.call(/^sf/)); //[object RegExp] console.log(Object.prototype.toString.call("hello")); //[object String] console.log(Object.prototype.toString.call(true)); //[object Boolean] console.log(Object.prototype.toString.call(null)); //[object Null] console.log(Object.prototype.toString.call(undefined)); //[object Undefined] console.log(Object.prototype.toString.call(function() {})); //[object Function] console.log(typeof(Object.prototype.toString.call(function() {}))); //string </script>
- JS中檢測(cè)數(shù)據(jù)類型的幾種方式及優(yōu)缺點(diǎn)小結(jié)
- 淺談javascript的數(shù)據(jù)類型檢測(cè)
- 關(guān)于JS數(shù)據(jù)類型檢測(cè)的多種方式總結(jié)
- JavaScript中檢測(cè)數(shù)據(jù)類型的四種方法
- javascript基本數(shù)據(jù)類型及類型檢測(cè)常用方法小結(jié)
- 在javaScript中檢測(cè)數(shù)據(jù)類型的幾種方式小結(jié)
- JavaScript數(shù)據(jù)類型檢測(cè)代碼分享
- js學(xué)習(xí)總結(jié)_基于數(shù)據(jù)類型檢測(cè)的四種方式(必看)
- JS數(shù)組索引檢測(cè)中的數(shù)據(jù)類型問題詳解
- js中各種數(shù)據(jù)類型檢測(cè)和判定的實(shí)戰(zhàn)示例
相關(guān)文章
javascript:void(0)的真正含義實(shí)例分析
void操作符解釋2008-08-08如何利用原生JS實(shí)時(shí)監(jiān)聽input框輸入值
這篇文章主要介紹了如何利用原生JS實(shí)時(shí)監(jiān)聽input框輸入值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01js實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)簡(jiǎn)單實(shí)例
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)簡(jiǎn)單實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01confirm的用法示例用于按鈕操作時(shí)確定是否執(zhí)行
這篇文章主要介紹了confirm的用法,confirm一般用于按鈕操作時(shí)確定是否執(zhí)行,需要的朋友可以參考下2014-06-06js獲取form表單所有數(shù)據(jù)的簡(jiǎn)單方法
下面小編就為大家?guī)硪黄猨s獲取form表單所有數(shù)據(jù)的簡(jiǎn)單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08