一文搞懂JavaScript中的typeof用法

基礎(chǔ)
因此,掌握該運算符的特點,對于寫出好的代碼,就會起到很大的幫助作用。
typeof operandtypeof(operand)
可能返回的類型字符串有:string, boolean, number, bigint, symbol, undefined, function, object。
返回類型
將根據(jù)可能的返回類型,進(jìn)行以下的分類介紹,對typeof的使用方法一網(wǎng)打盡。
string 和 boolean
字符串、布爾值分別返回 string、boolean。
包括 String()?和 Boolean()。
typeof '1' // 'string'typeof String(1) // 'string'typeof true // 'boolean'typeof Boolean() // 'boolean'
number 和 bigint
數(shù)字返回 number,包括 Number()、NaN 和 Infinity 等,以及 Math 對象下的各個數(shù)學(xué)常量值。
BigInt 數(shù)字類型值返回 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 等瀏覽器的非標(biāo)準(zhǔn)特性。
typeof undefined // 'undefined'typeof ttttttt // 'undefined'typeof document.all // 'undefined'
function
函數(shù)返回 function。
包括使用es6的 class 類聲明的。
還有各個內(nèi)置對象 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
對象、數(shù)組、null、正則表達(dá)式,都返回 object。
包括 Math、jsON 對象本身。
還有使用 new 操作符的數(shù)據(jù),除了 Function 以外。
typeof {} // 'object'typeof [] // 'object'typeof null // 'object'typeof /d/ // 'object'typeof Math // 'object'typeof new Number(1) // 'object'
其他
關(guān)于其他大部分的 JavaScript關(guān)鍵字,得到的結(jié)果值都是 object 或 function。
注:多數(shù)小寫字母開頭的是對象 object,多數(shù)大寫字母開頭的都是方法 function。常見的明確知道的方法不算,如 alert,prompt 等方法
除此以外,還有各js環(huán)境下具體實現(xiàn)的宿主對象。
常見問題
引用錯誤
在 let 和 const 塊級作用域變量定義之前,使用 typeof 會拋錯 ReferenceError。
因為塊級作用域變量,會在頭部形成?暫存死區(qū),直到被初始化,否則會報引用錯誤。
typeof tlet t = 1// VM327:1 Uncaught ReferenceError: t is not defined// at:1:1
如果是使用 var 定義變量,不會報錯,返回 undefined 。
有變量提升,不會形成暫時死區(qū)。
typeof null
對于 typeof null === 'object'?,記住即可,可能的解釋:
在JavaScript 最初的實現(xiàn)中,JavaScript 中的值是由一個表示類型的標(biāo)簽和實際數(shù)據(jù)值表示的。對象的類型標(biāo)簽是 0。由于null代表的是空指針(大多數(shù)平臺下值為 0x00),因此,null 的類型標(biāo)簽是 0,typeof null 也因此返回 "object"。
typeof 的局限性
typeof 的局限性,在于無法精確判斷出 null、數(shù)組、對象、正則?的類型。
所以如果要精準(zhǔn)判斷,還需要使用其他技術(shù)手段,或組合判斷。
如下,判斷數(shù)組類型:
Object.prototype.toString.call([]) // '[object Array]'[] instanceof Array // true[].constructor === Array // true
其中,Object.prototype.toString.call 是javascript中用于準(zhǔn)確判斷數(shù)據(jù)類型的通用手段。
擴(kuò)展:BigInt 類型
BigInt 來自于 ES11 增加的一種最新的基礎(chǔ)類型,可以用任意精度表示整數(shù)。
它提供了一種表示大于 2^53 - 1 整數(shù)的方法,能表示任意大的整數(shù)。
它是通過在整數(shù)末尾附加 n 或調(diào)用構(gòu)造函數(shù) BigInt()?來創(chuàng)建的。
IE 不支持。
10nBigInt(99) // 99n
注意點:
BigInt 能使用運算符?+、*、-、**和%。
除?>>> (無符號右移)?之外的?位操作?也可以支持。因為BigInt 都是有符號的。
BigInt 不支持單目 (+) 運算符,會報類型錯誤。
不能對 BigInt 使用 Math 對象中的方法。
BigInt 不能與 Number數(shù)字?進(jìn)行混合計算,否則,將拋出 TypeError。
在將 BigInt 轉(zhuǎn)換為 Boolean 時,它的行為類似 Number數(shù)字?。
BigInt 變量在轉(zhuǎn)換成 Number 變量時可能會丟失精度。
typeof 操作時返回 bigint。
使用 Object、String 等內(nèi)置對象轉(zhuǎn)換時,類似于 Number數(shù)字。
BigInt 使用?/?除操作時,帶小數(shù)的運算會被取整。
Number 和 BigInt 可以進(jìn)行比較,非嚴(yán)格相等。
JSON.stringify 處理 BigInt 會引發(fā)類型錯誤。
學(xué)習(xí)更多技能
請點擊下方公眾號
![]()

