JS中的四種數(shù)據(jù)類型判斷方法
本文總結(jié)了四種判斷方法:
1、typeof
typeof
是一個(gè)運(yùn)算符,其有兩種使用方式:(1)typeof
(表達(dá)式); (2)typeof
變量名;返回值是一個(gè)字符串,用來說明變量的數(shù)據(jù)類型;所以可以用此來判斷number
, string
, object
, boolean
, function
, undefined
, symbol
這七種類型,每種情況返回的內(nèi)容如下表所示:
// 字符串 console.log(typeof('lili')); // string // 數(shù)字 console.log(typeof(1)); // number // 布爾值 console.log(typeof(true)); // boolean // undefined console.log(typeof(undefined)); // undefined // 對(duì)象 console.log(typeof({})); // object // 數(shù)組 console.log(typeof([])); // object // null console.log(typeof(null)); // object // 函數(shù) console.log(typeof(() => {})); // function // Symbol值 console.log(typeof(Symbol())); // symbol
2、instanceof
instanceof
運(yùn)算符用于檢測(cè)構(gòu)造函數(shù)的 prototype
屬性是否出現(xiàn)在某個(gè)實(shí)例對(duì)象的原型鏈上,返回值為布爾值,用于指示一個(gè)變量是否屬于某個(gè)對(duì)象的實(shí)例。其語(yǔ)法如下所示:
object instanceof constructor
const arr = [1, 2]; // 判斷Object的prototype有沒有在數(shù)組的原型鏈上 console.log(arr instanceof Object); // true // 數(shù)組arr的原型 const proto1 = Object.getPrototypeOf(arr); console.log(proto1); // [] // 數(shù)組arr的原型的原型 const proto2 = Object.getPrototypeOf(proto1); console.log(proto2); // [] // Object的prototype console.log(Object.prototype); // 判斷arr的原型是否與Object的prototype相等 console.log(proto1 === Object.prototype); // false // 判斷arr的原型的原型是否與Object的prototype相等 console.log(proto2 === Object.prototype); // true
3、constructor
該種判斷方式其實(shí)涉及到原型、構(gòu)造函數(shù)和實(shí)例之間的關(guān)系,更深層次的講解將放到后面的內(nèi)容,下面只需要簡(jiǎn)單了解一下這三者關(guān)系即可。
在定義一個(gè)函數(shù)(構(gòu)造函數(shù))的時(shí)候,JS引擎會(huì)為其添加prototype
原型,原型上有其對(duì)應(yīng)的constructor
屬性指向該構(gòu)造函數(shù),從而原型和構(gòu)造函數(shù)之間互相知道對(duì)方。當(dāng)構(gòu)造函數(shù)實(shí)例化的時(shí)候,會(huì)產(chǎn)生對(duì)應(yīng)的實(shí)例,其實(shí)例可以訪問對(duì)應(yīng)原型上的constructor
屬性,這樣該實(shí)例就可以了解到通過誰產(chǎn)生了自己,這樣就可以在新對(duì)象產(chǎn)生之后了解其數(shù)據(jù)類型。
const val1 = 1; console.log(val1.constructor); // [Function: Number] const val2 = 'abc'; console.log(val2.constructor); // [Function: String] const val3 = true; console.log(val3.constructor); // [Function: Boolean]
雖然該方法可以判斷其數(shù)據(jù)類型,但存在兩個(gè)缺點(diǎn):
null
和undefined
是無效的對(duì)象,因此是不會(huì)有constructor
存在的,這兩種類型的數(shù)據(jù)需要通過其他方式來判斷。- 函數(shù)的
constructor
是不穩(wěn)定的,這個(gè)主要體現(xiàn)在自定義對(duì)象上,當(dāng)開發(fā)者重寫prototype
后,原有的constructor
引用會(huì)丟失,constructor
會(huì)默認(rèn)為Object
4、toString()
toString()
是 Object
的原型方法,調(diào)用該方法,默認(rèn)返回當(dāng)前對(duì)象的 [[Class]]
。這是一個(gè)內(nèi)部屬性,其格式為[object Xxx]
,其中 Xxx 就是對(duì)象的類型。所以利用Object.prototype.toString()
方法可以對(duì)變量的類型進(jìn)行比較準(zhǔn)確的判斷。
該類型針對(duì)不同不同變量的類型返回的結(jié)果如下所示:
利用該方法很容易構(gòu)建一個(gè)鑒型函數(shù),代碼如下所示:
function type(target) { const ret = typeof(target); const template = { "[object Array]": "array", "[object Object]":"object", "[object Number]":"number - object", "[object Boolean]":"boolean - object", "[object String]":'string-object' } if(target === null) { return 'null'; } else if(ret == "object"){ const str = Object.prototype.toString.call(target); return template[str]; } else{ return ret; } }
console.log(type({})); // object console.log(type(123)); // number console.log(type('123')); // string
到此這篇關(guān)于JS中的四種數(shù)據(jù)類型判斷方法的文章就介紹到這了,更多相關(guān)JS中的數(shù)據(jù)類型判斷方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
微信小程序頁(yè)面開發(fā)注意事項(xiàng)整理
這篇文章主要介紹了微信小程序頁(yè)面開發(fā)注意事項(xiàng)整理的相關(guān)資料,需要的朋友可以參考下2017-05-05微信小程序之選項(xiàng)卡的實(shí)現(xiàn)方法
這篇文章主要介紹了 微信小程序之選項(xiàng)卡的實(shí)現(xiàn)方法的相關(guān)資料,希望大家通過本文能實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09前端算法leetcode109題解有序鏈表轉(zhuǎn)換二叉搜索樹
這篇文章主要為大家介紹了前端算法leetcode109題解有序鏈表轉(zhuǎn)換二叉搜索樹示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Canvas如何判斷點(diǎn)在形狀內(nèi)及內(nèi)置API性能詳解
這篇文章主要為大家介紹了Canvas如何判斷點(diǎn)在形狀內(nèi)及內(nèi)置API性能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03微信小程序 scroll-view組件實(shí)現(xiàn)列表頁(yè)實(shí)例代碼
這篇文章主要介紹了微信小程序 scroll-view組件實(shí)現(xiàn)列表頁(yè)實(shí)例代碼的相關(guān)資料,scroll-view組件介紹scroll-view是微信小程序提供的可滾動(dòng)視圖組件,其主要作用是可以用來做手機(jī)端經(jīng)常會(huì)看到的上拉加載 ,需要的朋友可以參考下2016-12-12微信小程序 flex實(shí)現(xiàn)導(dǎo)航實(shí)例詳解
這篇文章主要介紹了微信小程序 flex實(shí)現(xiàn)導(dǎo)航實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04