一文搞懂js中的typeof用法
- 2021 年 11 月 8 日
- 筆記
基礎
typeof
運算符是 javascript
的基礎知識點,儘管它存在一定的局限性(見下文),但在前端js的實際編碼過程中,仍然是使用比較多的類型判斷方式。
因此,掌握該運算符的特點,對於寫出好的代碼,就會起到很大的幫助作用。
typeof 返回一個字符串,表示該操作值的數據類型,基本語法:
typeof operand
typeof(operand)
可能返回的類型字符串有:string
, boolean
, number
, bigint
, symbol
, undefined
, function
, object
。
返回類型
將根據可能的返回類型,進行以下的分類介紹,對typeof的使用方法一網打盡。
string 和 boolean
字符串、布爾值分別返回 string
、boolean
。
包括 String()
和 Boolean()
。
typeof '1' // 'string'
typeof String(1) // 'string'
typeof true // 'boolean'
typeof Boolean() // 'boolean'
number 和 bigint
數字返回 number
,包括 Number()
、NaN
和 Infinity
等,以及 Math
對象下的各個數學常量值。
BigInt
數字類型值返回 bigint
,包括 BigInt(1)
。
typeof 1 // 'number'
typeof NaN // 'number'
typeof Math.PI // 'number'
typeof 42n // 'bigint'
typeof BigInt(1) // 'bigint'
symbol
symbol 值返回 symbol
,包括 Symbol()
。
typeof Symbol() // 'symbol'
typeof Symbol('foo') // 'symbol'
typeof Symbol.iterator // 'symbol'
undefined
undefined 本身返回 undefined
。
不存在的,或者定義了但未賦初值的變量,都會返回 undefined
。
還有 document.all
等瀏覽器的非標準特性。
typeof undefined // 'undefined'
typeof ttttttt // 'undefined'
typeof document.all // 'undefined'
function
函數返回 function
。
包括使用es6的 class
類聲明的。
還有各個內置對象 String
、Number
、BigInt
、Boolean
、RegExp
、Error
、Object
、Date
、Array
、Function
、Symbol
本身。
以及 Function()
,new Function()
。
function func () {}
typeof func // 'function'
typeof class cs {} // 'function'
typeof String // 'function'
typeof RegExp // 'function'
typeof new Function() // 'function'
object
對象
、數組
、null
、正則表達式
,都返回 object
。
包括 Math
、JSON
對象本身。
還有使用 new
操作符的數據,除了 Function
以外。
typeof {} // 'object'
typeof [] // 'object'
typeof null // 'object'
typeof /d/ // 'object'
typeof Math // 'object'
typeof new Number(1) // 'object'
其他
關於其他大部分的 javascript關鍵字
,得到的結果值都是 object
或 function
。
註:多數小寫字母開頭的是對象 object
,多數大寫字母開頭的都是方法 function
。常見的明確知道的方法不算,如 alert
,prompt
等方法
除此以外,還有各js環境下具體實現的宿主對象。
常見問題
引用錯誤
在 let
和 const
塊級作用域變量定義之前,使用 typeof
會拋錯 ReferenceError
。
因為塊級作用域變量,會在頭部形成 暫存死區
,直到被初始化,否則會報引用錯誤。
typeof t
let t = 1
// VM327:1 Uncaught ReferenceError: t is not defined
// at <anonymous>:1:1
如果是使用 var
定義變量,不會報錯,返回 undefined
。
有變量提升,不會形成暫時死區。
typeof null
對於 typeof null === 'object'
,記住即可,可能的解釋:
在JavaScript 最初的實現中,JavaScript 中的值是由一個表示類型的標籤和實際數據值表示的。對象的類型標籤是 0。由於null代表的是空指針(大多數平台下值為 0x00),因此,null 的類型標籤是 0,typeof null 也因此返回 “object”。
typeof 的局限性
typeof
的局限性,在於無法精確判斷出 null、數組、對象、正則
的類型。
所以如果要精準判斷,還需要使用其他技術手段,或組合判斷。
如下,判斷數組類型:
Object.prototype.toString.call([]) // '[object Array]'
[] instanceof Array // true
[].constructor === Array // true
其中,Object.prototype.toString.call
是javascript中用於準確判斷數據類型的通用手段。
擴展:BigInt 類型
BigInt
來自於 ES11
增加的一種最新的基礎類型,可以用任意精度表示整數。
它提供了一種表示大於 2^53 - 1
整數的方法,能表示任意大的整數。
它是通過在整數末尾附加 n
或調用構造函數 BigInt()
來創建的。
IE
不支持。
10n
BigInt(99) // 99n
注意點:
BigInt
能使用運算符+、*、-、**和%
。- 除
>>> (無符號右移)
之外的位操作
也可以支持。因為BigInt
都是有符號的。 BigInt
不支持單目 (+) 運算符,會報類型錯誤。- 不能對
BigInt
使用Math
對象中的方法。 BigInt
不能與Number數字
進行混合計算,否則,將拋出TypeError
。- 在將
BigInt
轉換為Boolean
時,它的行為類似Number數字
。 BigInt
變量在轉換成Number
變量時可能會丟失精度。typeof
操作時返回bigint
。- 使用
Object、String
等內置對象轉換時,類似於Number數字
。 BigInt
使用/
除操作時,帶小數的運算會被取整。Number
和BigInt
可以進行比較,非嚴格相等。JSON.stringify
處理BigInt
會引發類型錯誤。