基礎
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會引發型別錯誤,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/353192.html
標籤:JavaScript
上一篇:web前端學習必備,10分鐘掌握前端CSS,詳解CSS的三大特性
下一篇:JS呼叫堆疊
