一、數(shù)據(jù)類型
js的數(shù)據(jù)類型分為兩類 基礎(chǔ)數(shù)據(jù)類型與引用數(shù)據(jù)類
- 基礎(chǔ)數(shù)據(jù)類型 undefined、null、number、string、 boolean、symbol(es6新增 表示獨(dú)一無(wú)二的值)
- 引用數(shù)據(jù)類型 Object、Function、 Array、Date、RegExp、Error、Number、String、Boolean
區(qū)別
-
基礎(chǔ)數(shù)據(jù)類型的數(shù)據(jù)名、值都保存在棧內(nèi)存中
icon.png -
引用數(shù)據(jù)類型的數(shù)據(jù) 名存儲(chǔ)在棧內(nèi)存中,值存儲(chǔ)在堆內(nèi)存中,但是棧內(nèi)存會(huì)提供一個(gè)引用的地址指向堆內(nèi)存中的值
icon2.png
二、數(shù)據(jù)類型判斷
1、typeof()
判斷基本數(shù)據(jù)類型(也可用于引用類型的Function類型判斷)
console.log(typeof 123) //number
console.log(typeof '123') //string
console.log(typeof false) //boolean
console.log(typeof undefined) //undefined
console.log(typeof null) //object 無(wú)效
console.log(typeof Symbol() ) //symbol
console.log(typeof {}) //object
console.log(typeof []) //object 無(wú)效
console.log(typeof Function() ) //function
console.log(typeof new Date()) //object 無(wú)效
console.log(typeof new RegExp()) //object 無(wú)效
console.log(typeof new Number()) //object 無(wú)效
console.log(typeof new String()) //object 無(wú)效
console.log(typeof new Boolean()) //object 無(wú)效
typeof 可以檢測(cè)出 number、string、boolean、undefined、object、function 、symbol七種數(shù)據(jù)類型,對(duì)于Array、null、以及其他構(gòu)造器對(duì)象的類型檢測(cè)無(wú)效,原因是構(gòu)造器本質(zhì)都是object類型。特殊的是null的數(shù)據(jù)類型是object 是因?yàn)閚ull會(huì)被當(dāng)作一個(gè)空對(duì)象的引用
2、instanceof
判斷引用數(shù)據(jù)類型
console.log([] instanceof Array) //true
console.log({} instanceof Object) //true
console.log(Function() instanceof Function) //true
console.log(new Date() instanceof Date) //true
console.log(new RegExp() instanceof RegExp) //true
console.log(new Number() instanceof Number) //true
console.log(null instanceof Null) //報(bào)錯(cuò)
console.log(undefined instanceof undefined) //報(bào)錯(cuò)
...
表達(dá)式:a instanceof b,用來(lái)判斷a是否是b的實(shí)例對(duì)象
其中 a是對(duì)象 b是函數(shù),通過(guò)判斷構(gòu)造函數(shù)b的原型對(duì)象 b.prototype是否在a實(shí)例對(duì)象的原型鏈上來(lái)確定實(shí)例對(duì)象的具體數(shù)據(jù)類型,但不能用來(lái)檢測(cè) null undefined
3、constructor
判斷引用數(shù)據(jù)類型
constructor 屬性返回對(duì)創(chuàng)建此對(duì)象的數(shù)組函數(shù)的引用,就是返回對(duì)象相對(duì)應(yīng)的構(gòu)造函數(shù)
console.log([].constructor) //? Array() { [native code] }
console.log({}.constructor) //? Object() { [native code] }
console.log(Function().constructor) //? Function() { [native code] }
console.log(new Date().constructor) //? Date() { [native code] }
console.log(new RegExp().constructor) //? RegExp() { [native code] }
console.log(new Number().constructor) //? Number() { [native code] }
console.log(null.constructor) //報(bào)錯(cuò)
console.log(undefined.constructor) //報(bào)錯(cuò)
與instanceof定義差異
a instanceof b : 判斷a是否是構(gòu)造函數(shù)b的實(shí)例對(duì)象
a.constructor === b : 判斷a的構(gòu)造函數(shù) 是否是b
同時(shí)他也不能用于檢測(cè) null 與 undefined的數(shù)據(jù)類型
注:
使用instaceof和construcor,被判斷的array必須是在當(dāng)前頁(yè)面聲明的!比如,一個(gè)頁(yè)面(父頁(yè)面)有一個(gè)框架,框架中引用了一個(gè)頁(yè)面(子頁(yè)面),在子頁(yè)面中聲明了一個(gè)array,并將其賦值給父頁(yè)面的一個(gè)變量,這時(shí)判斷該變量,Array == object.constructor;會(huì)返回false;
原因:
1、array屬于引用型數(shù)據(jù),在傳遞過(guò)程中,僅僅是引用地址的傳遞。
2、每個(gè)頁(yè)面的Array原生對(duì)象所引用的地址是不一樣的,在子頁(yè)面聲明的array,所對(duì)應(yīng)的構(gòu)造函數(shù),是子頁(yè)面的Array對(duì)象;父頁(yè)面來(lái)進(jìn)行判斷,使用的Array并不等于子頁(yè)面的Array;切記 參考:http://www.cnblogs.com/leejersey/p/5191924.html
4、Object.prototype.toString.call()
可以用來(lái)判斷基礎(chǔ)數(shù)據(jù)類型和引用數(shù)據(jù)類型,提供了一個(gè)通用的數(shù)據(jù)類型判斷模式
Object.prototype.toString.call('') //[object String]
Object.prototype.toString.call(1) //[object Number]
Object.prototype.toString.call(true) //[object Boolean]
Object.prototype.toString.call(null) //[object Null]
Object.prototype.toString.call(undefined) //[object Undefined]
Object.prototype.toString.call({}) //[object Object]
Object.prototype.toString.call([]) //[object Array]
Object.prototype.toString.call(new Function()) //[object Function]
Object.prototype.toString.call(new Date()) //[object Date]
Object.prototype.toString.call(new RegExp()) //[object RegExp]
Object.prototype.toString.call(new Error()) //[object Error]
Object.prototype.toString.call(Math) //[object Math]
Object.prototype.toString.call(JSON) //[object JSON]
Object.prototype.toString.call(Symbol()) //[object Symbol]
注:通過(guò)Object.prototype.toString.call()來(lái)檢測(cè)對(duì)象類型的原理
toString()方法返回反映這個(gè)對(duì)象的字符串,那為什么不用obj.toString()直接檢測(cè)對(duì)象的類型呢?
console.log([1, 2].toString()) //1,2
console.log((123).toString()) //123
console.log('abc'.toString()) //abc
console.log(new Date().toString()) //Sat Aug 03 2019 17:29:54 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
console.log(function(){}.toString()) //function(){}
console.log(/\d/.toString()) // /\d/
console.log(new Error().toString()) //Error
由上可以看出直接通過(guò)obj.toString()輸出與Object.prototype.toString.call(obj)的結(jié)果不一致。
這是因?yàn)閠oString為Object的原型方法,而Array 、Function等類型作為Object的實(shí)例,都重寫了toString方法。不同的對(duì)象類型調(diào)用toString方法時(shí),根據(jù)原型鏈的知識(shí),調(diào)用的是對(duì)應(yīng)的重寫之后的toString方法(Function類型返回內(nèi)容為函數(shù)體的字符串,Array類型返回元素組成的字符串.....),而不會(huì)去調(diào)用Object上原型toString方法(返回對(duì)象的具體類型),所以采用obj.toString()不能得到其對(duì)象類型,只能將obj轉(zhuǎn)換為字符串類型;因此,在想要得到對(duì)象的具體類型時(shí),應(yīng)該調(diào)用Object上原型toString方法
我們可以驗(yàn)證一下,將數(shù)組的toString方法刪除,看看會(huì)是什么結(jié)果?
var arr=[1,2,3];
console.log(Array.prototype.hasOwnProperty("toString"));//true
console.log(arr.toString());//1,2,3
delete Array.prototype.toString;//delete操作符可以刪除實(shí)例屬性
console.log(Array.prototype.hasOwnProperty("toString"));//false
console.log(arr.toString());//"[object Array]"
刪除了Array的toString方法后,同樣再采用arr.toString()方法調(diào)用時(shí),不再有屏蔽Object原型方法的實(shí)例方法,因此沿著原型鏈,arr最后調(diào)用了Object的toString方法,返回了和Object.prototype.toString.call(arr)相同的結(jié)果。
參考:https://www.cnblogs.com/youhong/p/6209054.html
三、思考
- 區(qū)分Array 與Object的數(shù)據(jù)類型
function isArray(object){
return object && typeof object==='object' && Array == object.constructor
}
function isArray(object){
return object && typeof object==='object' && object instanceof Array
}
function isArray(object){
return object && typeof object==='object' && Array.isArray(object)
}
- 檢測(cè)null數(shù)據(jù)類型的方法
function isNull(object){
return Object.prototype.toString.call(object) === '[object Null]'
}
function isNull(object){
return object === null
}