1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        一文搞懂JavaScript中的typeof用法

        共 3082字,需瀏覽 7分鐘

         ·

        2021-11-15 18:43


        基礎(chǔ)

        typeof 運(yùn)算符是 JavaScript 的基礎(chǔ)知識(shí)點(diǎn),盡管它存在一定的局限性(見下文),但在前端js的實(shí)際編碼過程中,仍然是使用比較多的類型判斷方式。
        因此,掌握該運(yùn)算符的特點(diǎn),對(duì)于寫出好的代碼,就會(huì)起到很大的幫助作用。
        typeof?返回一個(gè)字符串,表示該操作值的數(shù)據(jù)類型,基本語法:
        typeof operandtypeof(operand)

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

        返回類型

        將根據(jù)可能的返回類型,進(jìn)行以下的分類介紹,對(duì)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 對(duì)象下的各個(gè)數(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。
        不存在的,或者定義了但未賦初值的變量,都會(huì)返回 undefined。
        還有 document.all 等瀏覽器的非標(biāo)準(zhǔn)特性。

        typeof undefined // 'undefined'typeof ttttttt // 'undefined'typeof document.all // 'undefined'

        function

        函數(shù)返回 function。
        包括使用es6的 class 類聲明的。
        還有各個(gè)內(nèi)置對(duì)象 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

        對(duì)象、數(shù)組、null、正則表達(dá)式,都返回 object。
        包括 Math、jsON 對(duì)象本身。
        還有使用 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ù)小寫字母開頭的是對(duì)象 object,多數(shù)大寫字母開頭的都是方法 function。常見的明確知道的方法不算,如 alert,prompt 等方法

        除此以外,還有各js環(huán)境下具體實(shí)現(xiàn)的宿主對(duì)象。

        常見問題

        引用錯(cuò)誤

        在 let 和 const 塊級(jí)作用域變量定義之前,使用 typeof 會(huì)拋錯(cuò) ReferenceError。
        因?yàn)閴K級(jí)作用域變量,會(huì)在頭部形成?暫存死區(qū),直到被初始化,否則會(huì)報(bào)引用錯(cuò)誤。

        typeof tlet t = 1// VM327:1 Uncaught ReferenceError: t is not defined//    at :1:1

        如果是使用 var 定義變量,不會(huì)報(bào)錯(cuò),返回 undefined 。
        有變量提升,不會(huì)形成暫時(shí)死區(qū)。

        typeof null

        對(duì)于 typeof null === 'object'?,記住即可,可能的解釋:
        在JavaScript 最初的實(shí)現(xiàn)中,JavaScript 中的值是由一個(gè)表示類型的標(biāo)簽和實(shí)際數(shù)據(jù)值表示的。對(duì)象的類型標(biāo)簽是 0。由于null代表的是空指針(大多數(shù)平臺(tái)下值為 0x00),因此,null 的類型標(biāo)簽是 0,typeof null 也因此返回 "object"。

        typeof 的局限性

        typeof 的局限性,在于無法精確判斷出 null、數(shù)組、對(duì)象、正則?的類型。
        所以如果要精準(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

        注意點(diǎn):

        • BigInt 能使用運(yùn)算符?+、*、-、**和%。

        • 除?>>> (無符號(hào)右移)?之外的?位操作?也可以支持。因?yàn)锽igInt 都是有符號(hào)的。

        • BigInt 不支持單目 (+) 運(yùn)算符,會(huì)報(bào)類型錯(cuò)誤。

        • 不能對(duì) BigInt 使用 Math 對(duì)象中的方法。

        • BigInt 不能與 Number數(shù)字?進(jìn)行混合計(jì)算,否則,將拋出 TypeError。

        • 在將 BigInt 轉(zhuǎn)換為 Boolean 時(shí),它的行為類似 Number數(shù)字?。

        • BigInt 變量在轉(zhuǎn)換成 Number 變量時(shí)可能會(huì)丟失精度。

        • typeof 操作時(shí)返回 bigint。

        • 使用 Object、String 等內(nèi)置對(duì)象轉(zhuǎn)換時(shí),類似于 Number數(shù)字。

        • BigInt 使用?/?除操作時(shí),帶小數(shù)的運(yùn)算會(huì)被取整。

        • Number 和 BigInt 可以進(jìn)行比較,非嚴(yán)格相等。

        • JSON.stringify 處理 BigInt 會(huì)引發(fā)類型錯(cuò)誤。



        學(xué)習(xí)更多技能

        請(qǐng)點(diǎn)擊下方公眾號(hào)

        瀏覽 33
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            插菊花综合 | 男人添女人囗交视频 | 2一3sex性hd | ass粉嫩中国小美女pics | 国产亲子伦在线观看 | brazzersfreehdxxxxpron | A片在线免费观看视频 | 中文字幕国产精品视频 | 亚洲天堂在线视频观看 | 黑丝操逼 |