方法
1、typeof 運(yùn)算符
2、instanceof 運(yùn)算符
instanceof 嚴(yán)格來說是 Java 中得一個(gè)雙目運(yùn)算符,用來測試一個(gè)對象是否為一個(gè)類得實(shí)例,用法為:
3、Object.prototype.toString方法
(1)在 Javascript 里使用 typeof 來判斷數(shù)據(jù)類型,只能區(qū)分基本類型,即 “number”,”string”,”undefined”,”boolean”,”object”,“function”,“symbol”(ES6 新增)七種。
(2)對于數(shù)組、null、對象來說,其關(guān)系錯(cuò)綜復(fù)雜,使用typeof 都會(huì)統(tǒng)一返回 “object” 字符串。
(3)要想?yún)^(qū)別對象、數(shù)組、函數(shù)單純使用 typeof 是不行得,Javascript 中,通過Object.prototype.toString 方法,判斷某個(gè)對象值屬于哪種內(nèi)置類型。
(4)在介紹 Object.prototype.toString 方法之前,我們先把 toString()方法和Object.prototype.toString.call()方法進(jìn)行對比。
(5)toString()方法和 Object.prototype.toString.call()方法對比。