JS數(shù)據(jù)類型的四種判斷方式

數(shù)據(jù)類型

基本類型:String、Number、Boolean、Undefined、Null 。由于其占據(jù)空間固定,是簡單的數(shù)據(jù)段,為了便于提升變量查詢速度,將其存儲在棧中,即按值訪問。

引用類型:Object。由于其值的大小會改變,所以不能將其存放在棧中,否則會降低變量查詢速度,因此,其值存儲在堆ject。由于其值的大小會改變,所以不能將其存放在棧中,否則會降低變量查詢速度,因此,其值存儲在堆(heap)中,而存儲在變量處的值,是一個指針,指向存儲對象的內(nèi)存處,即按址訪問。引用類型除 Object 外,還包括 Function 、Array、RegExp、Date 等等。


1. typeof


typeof("haha")? // "string"

typeof(222) // "number"

typeof(NaN) // "number"

typeof(true) // "boolean"

typeof(false) // "boolean"

typeof(null) // "object"

typeof(undefined) // "undefined"



typeof({}) // "object"

typeof([]) // "object"

typeof(function(){}) // "function"

typeof(()=>{}) //"function"

typeof(/2/) //"object"

typeof(new RegExp()) //"object"

typeof(new Date()) //"object"


無法識別出object、null、array、正則、日期等,

可以用于判斷一個變量是否為函數(shù),是的話,執(zhí)行此函數(shù),避免執(zhí)行非函數(shù)導致程序報錯。



2、instanceof

用來判斷A是否為B的實例,語句:A instanceof B? 如果 A 是 B 的實例,則返回 true,否則返回 false。


[] instanceof Array; //true

{} instanceof Object;//true

new Date() instanceof Date;//true


function Person(){};

new Person() instanceof Person;


[] instanceof Object; //true

new Date() instanceof Object;//true


需要注意的是instanceof是基于原型鏈的判斷;

[].__proto__ === Array.prototype;

Array.prototype.__proto__ == Object.prototype;

所以[] instanceof Object為true

Date與正則同理。

綜上所述:instanceof常用于判斷變量是否為某個數(shù)據(jù)類型,例如判斷某個變量(a)是否為數(shù)組? a? instanceof Array即可;

另外instanceof 不能用于基本數(shù)據(jù)類型的判斷



3、constructor

{}.constructor === Object? //true

[].constructor === Array? //true

new Function().constructor === Function? //true

''.constructor === String? //true

var a = 2; a.constructor === Number? // true(2.constructor直接這么寫瀏覽器會報變量錯誤)

true.constructor === Boolean //true

false.constructor ===Boolean //true

null.constructor? // Cannot read property 'constructor' of null

undefined.constructor // Cannot read property 'constructor' of undefined

其實[],{},'',2,false,true 均無constructor屬性 都在各自的 __proto__中

根據(jù)上述打印結(jié)果:constructor 不可用于判斷 null,undefined,且會報錯


4.toString

Object.prototype.toString.call('') ;? // [object String]

Object.prototype.toString.call(1) ;? ? // [object Number]

Object.prototype.toString.call(true) ; // [object Boolean]

Object.prototype.toString.call(undefined) ; // [object Undefined]

Object.prototype.toString.call(null) ; // [object Null]

Object.prototype.toString.call(new Function()) ; // [object Function]

Object.prototype.toString.call(new Date()) ; // [object Date]

Object.prototype.toString.call([]) ; // [object Array]

Object.prototype.toString.call(new RegExp()) ; // [object RegExp]

Object.prototype.toString.call(new Error()) ; // [object Error]

Object.prototype.toString.call(document) ; // [object HTMLDocument]

Object.prototype.toString.call(window) ; //[object global] window是全局對象 global 的引用


此方法可以判斷所有的數(shù)據(jù)類型,但是必須通過 call 或 apply 來調(diào)用,不能直接用,而不能直接調(diào)用 toString ,

雖然,從原型鏈的角度講,所有對象的原型鏈最終都指向了 Object,

按照JS變量查找規(guī)則,其他對象應該也可以直接訪問到 Object 的 toString方法,但是事實上,大部分的對象都實現(xiàn)了自身的 toString 方法,

這樣就可能會導致 Object 的 toString 被終止查找,因此要用 call/apply 來強制調(diào)用Object 的 toString 方法。

例如:

Object.prototype.toString? === {}.toString? //true

Object.prototype.toString? === [].toString //false

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容