偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

一文搞懂JavaScript中的Typeof用法

開發(fā) 前端
typeof 運算符是 JavaScript 的基礎(chǔ)知識點,盡管它存在一定的局限性(見下文),但在前端js的實際編碼過程中,仍然是使用比較多的類型判斷方式。

 基礎(chǔ)

[[442869]]

typeof 運算符是 JavaScript 的基礎(chǔ)知識點,盡管它存在一定的局限性(見下文),但在前端js的實際編碼過程中,仍然是使用比較多的類型判斷方式。

因此,掌握該運算符的特點,對于寫出好的代碼,就會起到很大的幫助作用。

typeof 返回一個字符串,表示該操作值的數(shù)據(jù)類型,基本語法:

  1. typeof operand 
  2. typeof(operand) 

可能返回的類型字符串有:string, boolean, number, bigint, symbol, undefined, function, object。

返回類型

將根據(jù)可能的返回類型,進行以下的分類介紹,對typeof的使用方法一網(wǎng)打盡。

string 和 boolean

字符串、布爾值分別返回 string、boolean。包括 String() 和 Boolean()。

 

  1. typeof '1' // 'string' 
  2. typeof String(1) // 'string' 
  3. typeof true // 'boolean' 
  4. typeof Boolean() // 'boolean' 

 

number和bigint

數(shù)字返回 number,包括 Number()、NaN 和 Infinity 等,以及 Math 對象下的各個數(shù)學(xué)常量值。

BigInt 數(shù)字類型值返回 bigint,包括 BigInt(1)。

 

  1. typeof 1 // 'number' 
  2. typeof NaN // 'number' 
  3. typeof Math.PI // 'number' 
  4. typeof 42n // 'bigint' 
  5. typeof BigInt(1) // 'bigint' 

 

symbol

symbol 值返回 symbol,包括 Symbol()。

 

  1. typeof Symbol() // 'symbol' 
  2. typeof Symbol('foo') // 'symbol' 
  3. typeof Symbol.iterator // 'symbol' 

 

undefined

undefined 本身返回 undefined。

不存在的,或者定義了但未賦初值的變量,都會返回 undefined。

還有 document.all 等瀏覽器的非標(biāo)準(zhǔn)特性。

 

  1. typeof undefined // 'undefined' 
  2. typeof ttttttt // 'undefined' 
  3. 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()。

 

  1. function func () {} 
  2. typeof func // 'function' 
  3. typeof class cs {} // 'function' 
  4. typeof String // 'function' 
  5. typeof RegExp // 'function' 
  6. typeof new Function() // 'function' 

 

object

對象、數(shù)組、null、正則表達(dá)式,都返回 object。

包括 Math、jsON 對象本身。

還有使用 new 操作符的數(shù)據(jù),除了 Function 以外。

 

  1. typeof {} // 'object' 
  2. typeof [] // 'object' 
  3. typeof null // 'object' 
  4. typeof /d/ // 'object' 
  5. typeof Math // 'object' 
  6. 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ū),直到被初始化,否則會報引用錯誤。

 

  1. typeof t 
  2. let t = 1 
  3. // VM327:1 Uncaught ReferenceError: t is not defined 
  4. //    at <anonymous>:1:1 

如果是使用 var 定義變量,不會報錯,返回 undefined 。

有變量提升,不會形成暫時死區(qū)。

typeofnull

對于 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ù)組類型:

 

  1. Object.prototype.toString.call([]) // '[object Array]' 
  2.  
  3. [] instanceof Array // true 
  4.  
  5. [].constructor === Array // true 

 

其中,

Object.prototype.toString.call 是javascript中用于準(zhǔn)確判斷數(shù)據(jù)類型的通用手段。

擴展:BigInt類型

BigInt 來自于 ES11 增加的一種最新的基礎(chǔ)類型,可以用任意精度表示整數(shù)。

它提供了一種表示大于 2^53 - 1 整數(shù)的方法,能表示任意大的整數(shù)。

它是通過在整數(shù)末尾附加 n 或調(diào)用構(gòu)造函數(shù) BigInt() 來創(chuàng)建的。IE 不支持。

 

  1. 10n 
  2. BigInt(99) // 99n 

 

注意點:

  • BigInt 能使用運算符 +、*、-、**和%。
  • 除 >>> (無符號右移) 之外的 位操作 也可以支持。因為BigInt 都是有符號的。
  • BigInt 不支持單目 (+) 運算符,會報類型錯誤。
  • 不能對 BigInt 使用 Math 對象中的方法。
  • BigInt 不能與 Number數(shù)字 進行混合計算,否則,將拋出 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 可以進行比較,非嚴(yán)格相等。
  • JSON.stringify 處理 BigInt 會引發(fā)類型錯誤。

 

責(zé)任編輯:華軒 來源: 今日頭條
相關(guān)推薦

2022-08-15 15:39:23

JavaScript面向?qū)ο?/a>數(shù)據(jù)

2020-12-21 07:54:46

CountDownLa用法源碼

2024-11-19 13:20:55

2021-09-07 09:46:40

JavaScriptGenerator函數(shù)

2023-07-04 08:56:07

指針類型Golang

2024-04-12 12:19:08

語言模型AI

2022-03-24 08:51:48

Redis互聯(lián)網(wǎng)NoSQL

2021-10-11 10:19:48

Javascript 高階函數(shù)前端

2021-03-22 10:05:59

netstat命令Linux

2023-09-08 08:20:46

ThreadLoca多線程工具

2023-09-15 12:00:01

API應(yīng)用程序接口

2023-05-31 13:32:08

Javalambda函數(shù)

2021-09-11 10:41:27

PythonPickle模塊

2020-05-15 16:37:13

PowerBI數(shù)據(jù)分析

2022-05-05 16:47:24

Docker網(wǎng)絡(luò)空間容器

2021-09-28 07:12:10

avaScriptCurrying柯里化

2023-09-02 21:27:09

2021-03-04 00:09:31

MySQL體系架構(gòu)

2023-05-22 13:27:17

2021-02-28 20:53:37

Cookie存儲瀏覽器
點贊
收藏

51CTO技術(shù)棧公眾號