• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    js判斷數據類型的方法

    js判定數據類型常用的有以下三種:

    1、typeof:返回類型的字符串描述值,如“string”、“object”等。可是,typeof(null)返回object

    2、instanceof:判定類型是否是指心猿意馬的類型,返回true或者false。與C#的is關頭字近似

    3、$.type():返回類型的字符串描述值,如“string”。可是,$.type(null)返回的是“null”字符串

    4、對于上面第3項,需要引入jquery才可以利用,同時,$.type()可以或許返回更切確的類型

    方式/步調

    1. 1

      打開任一瀏覽器,新建一個空白頁,即深奧無極試typeof、instanceof兩種。

      此處以chrome瀏覽器為例,在瀏覽器界面按F12,進入開辟者模式,在【節制臺】或者叫做【Console】頁面輸入測試內容即可

    2. 2

      【typeof】可以返回的類型有以下6種:"undefined", "boolean", "number", "string", "object", "function"。演示代碼如下:

      1)typeof a                      測試成果:"undefined",當為界說a變量的時辰

      2)typeof true                 測試成果:"boolean"

      3)typeof 123                  測試成果:"number"

      4)typeof ""                     測試成果:"string"

      5)typeof [1,2]                 測試成果:"object"

      6)typeof function(){}      測試成果:"function"

    3. 3

      【instanceof】凡是用于typeof成果為object時,判定是否是具體系體例心猿意馬的類型(注重類型需要巨細寫切確),測試如下:

      1)數組類型 

           var arr = [1, 2, 3];

           arr instanceof Array

           測試成果:true

      2)日期類型

           var dt = new Date();

           dt instanceof Date

           測試成果:true

    4. 4

      【$.type()】此種體例需要引入jquery才能利用。

      此處以打開百度首頁節制臺演示

    5. 5

      【$.type()】測試如下:

      1)$.type()                       測試成果:"undefined"

      2)$.type(true)                 測試成果:"boolean"

      3)$.type(123)                  測試成果:"number"

      4)$.type("")                     測試成果:"string"

      5)$.type([1,2])                 測試成果:"array"

      6)$.type(function(){})      測試成果:"function"

    6. 6

      【$.type()】、【typeof】一部門返回成果的不同,此處接著上一步演示:

      1)null:$.type(null) = "null",typeof null = "object" 

      2)date類型:$.type(new Date()) = "date",typeof(new Date()) = "object"

      3)array類型:$.type([1, 2]) = "array",typeof([1, 2]) = "object"

    • 發表于 2018-10-15 00:00
    • 閱讀 ( 678 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    admin
    admin

    0 篇文章

    作家榜 ?

    1. xiaonan123 189 文章
    2. 湯依妹兒 97 文章
    3. luogf229 46 文章
    4. jy02406749 45 文章
    5. 小凡 34 文章
    6. Daisy萌 32 文章
    7. 我的QQ3117863681 24 文章
    8. 華志健 23 文章

    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆