JS 中分為七種內(nèi)置類型千所,七種內(nèi)置類型又分為兩大類型:基本類型和對象(Object)报咳。
- 基本類型:
null
,undefined
,boolean
,number
,string
,symbol
。 - 對象:
[]
,{}
等
判斷類型的方法
typeof
操作符返回一個字符串玖详,表示未經(jīng)計算的操作數(shù)的類型把介。
// 數(shù)值
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof NaN === 'number'; // 盡管它是 "Not-A-Number" (非數(shù)值) 的縮寫
typeof Number(1) === 'number'; // Number 會嘗試把參數(shù)解析成數(shù)值
typeof 42n === 'bigint';
//BigInt 是一種內(nèi)置對象,它提供了一種方法來表示大于 253 - 1 的整數(shù)
// 字符串
typeof 'bla' === 'string';
typeof String(1) === 'string'; // String 將任意值轉(zhuǎn)換為字符串竹宋,比 toString 更安全
// 布爾值
typeof true === 'boolean';
typeof Boolean(1) === 'boolean'; // Boolean() 會基于參數(shù)是真值還是虛值進(jìn)行轉(zhuǎn)換
// Symbols
typeof Symbol() === 'symbol';
// Undefined
typeof undefined === 'undefined';
typeof undeclaredVariable === 'undefined';
// 對象
typeof {a: 1} === 'object';
typeof [1, 2, 4] === 'object';
typeof new Date() === 'object';
typeof /regex/ === 'object'; // 在 JavaScript中劳澄,正則表達(dá)式也是對象
// null
typeof null === 'object'
// 函數(shù)
typeof function() {} === 'function';
typeof class C {} === 'function'
typeof Math.sin === 'function';
上述例子摘錄于MDN
,可以看出用typeof
判斷類型有許多問題,也可以引出幾個很有意思的點.
NaN
首先NaN
是一個很有意思的類型, NaN
不等于它自己.必須使用 Number.isNaN()
或 isNaN()
函數(shù)來進(jìn)行判斷地技。
NaN === NaN; // false
Number.NaN === NaN; // false
isNaN(NaN); // true
isNaN(Number.NaN); // true
// 注意isNaN()和Number.isNaN()的區(qū)別:如果當(dāng)前值是NaN蜈七,或者將其強制轉(zhuǎn)換為數(shù)字后將是NaN,則前者將返回true莫矗。而后者僅當(dāng)值當(dāng)前為NaN時才為true:
isNaN('hello world'); // true
Number.isNaN('hello world'); // false
其實很好理解,NaN
的定義為不是一個數(shù)字,而其他類型強制轉(zhuǎn)換為數(shù)字類型有時會失敗,例如Number('1a') !== Number('2b')
,雖然它們各自此時都為NaN
,但假如它們轉(zhuǎn)換成功必定是不相等的.
null
null
在類型判斷時為何會返回object
,這和JS
最初實現(xiàn)有關(guān),在 JS
的最初版本中飒硅,使用的是 32 位系統(tǒng),為了性能考慮使用低位存儲了變量的類型信息作谚,對象的類型標(biāo)簽是 0三娩。而由于 null
代表的是空指針(大多數(shù)平臺下值為 0x00),因此妹懒,null
的類型標(biāo)簽也是0雀监,typeof null
也因此返回 "object"
。
object.property.toString.call
MDN
中這樣描述:為了每個對象都能通過 Object.prototype.toString()
來檢測,需要以 Function.prototype.call()
或者 Function.prototype.apply()
的形式來調(diào)用会前,傳遞要檢查的對象作為第一個參數(shù)好乐,稱為 thisArg
。
Object.prototype.toString.call(1) // "[object Number]"
Object.prototype.toString.call(NaN) // "[object Number]"
Object.prototype.toString.call('') // "[object String]"
Object.prototype.toString.call(Symbol()) // "[object Symbol]"
Object.prototype.toString.call(42n) // "[object BigInt]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
Object.prototype.toString.call(true) // "[object Boolean]
Object.prototype.toString.call({a:1}) // "[object Object]"
Object.prototype.toString.call([1,2]) // "[object Array]"
Object.prototype.toString.call(new Date) // "[object Date]"
Object.prototype.toString.call(function(){}) // "[object Function]"
以下是個人理解,不一定正確:
toString()
方法返回一個表示該對象的字符串瓦宜。
而Object.prototype.toString()
返回的是[object Object]
,通過 call()
或者apply()
方法改變了this
的指向,指向了對應(yīng)參數(shù)的對象,所以第二個Object
變成了參數(shù)的類型.
大家可以參考一篇文章:https://juejin.im/post/591647550ce4630069df1c4a
總結(jié)
-
NaN
比較特殊,需要利用Number.isNaN()
或isNaN()
來判斷一個類型是否為NaN
-
null
因為歷史原因會被typeof
方法判斷為object
,可以利用Object.prototype.toString.call(null)
來進(jìn)行正確的判斷 - 其中
Symbol
類型也較為特殊,會另外用一篇博客來學(xué)習(xí)研究