天天看點

一文搞懂JavaScript中的typeof用法

一文搞懂JavaScript中的typeof用法

基礎

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 會引發類型錯誤。

學習更多技能

請點選下方公衆号

繼續閱讀