js判断数据类型

        一般在js判断数据类型时,主要有以下几种方法:typeof、instanceof、constructor、prototype、$.type/jQuery.type

        接下来我们来比较下各种方法的异同

        var a = 'string'

        typeof(a)  =====>string

        a.constructor == String  =====>true

        Object.prototype.toString.call(a)  =====>"[object String]"

        $.type(a)  =====>string

  

        var b = 420

        typeof(b)  =====>number

        b.constructor == Number  =====>true

        Object.prototype.toString.call(b)  =====>"[object Number]"

        $.type(b)  =====>number

        

        var c = [1990, 4, 20]

        typeof(c)  =====>object

        c instanceof Object  =====>true

        c.constructor == Array  =====>true

        Object.prototype.toString.call(c)  =====>"[object Array]"

        $.type(c)  =====>array

        var d = undefined

        typeof(d)  =====>undefined

        Object.prototype.toString.call(d)  ======>"[object Undefined]"

        $.type(d)  =====>undefined

        var e = null

        typeof(e)  =====>object

        e instanceof Object  =====>false

        Object.prototype.toString.call(e)  =====>"[object Null]"

        $.type(e)  =====>null

        var f = {a:'b'}

        typeof(f)  =====>object

        f instanceof Object  =====>true

        f.constructor == Object  =====>true

        Object.prototype.toString.call(f)  =====>"[object Object]"

        $.type(f)  =====>object

        var g = new Date()

        typeof(g)  =====>object

        g instanceof Object  =====>true

        g.constructor == Date  =====>true

        Object.prototype.toString.call(g)  =====>"[object Date]"

        $.type(g)  =====>date

        var h = function(){}

        typeof(h)  =====>function

        h instanceof Function  =====>true

        h.constructor == Function  =====>true

        Object.prototype.toString.call(h) =====>"[object Function]"

        $.type(h)  =====>function

        对于instanceof,一般是在我们已知数据类型时进行判断,但一定要注意类型名的大小写。对于constructor,我们一般要注意类继承的问题:

        function a () {}

        function b () {}

        a.prototype = new b() //a继承b

        var obj = new a()

        obj.constructor == a  =====>false

        obj.constructor == b  =====>true

        使用instanceof 不会出现该类问题:

        obj instanceof a  =====>true

        obj instanceof b  =====>true

        使用prototype时同样要注意大小写的问题。

        而$.type则是比较万能的方法了,可以判断布尔、error、正则等等

        $.type(false)  =====>boolean

        $.type(new Error())  =====>error

        $.type(/\d/)  =====>regexp

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在JavaScript中,可以使用typeof运算符来判断数据类型。typeof通常用于判断基本数据类型,返回表示数据类型的字符串。例如,typeof 1会返回"number",typeof 'a'会返回"string",typeof true会返回"boolean",typeof undefined会返回"undefined",typeof null会返回"object",typeof {}会返回"object",typeof [1,2,3会返回"object",typeof new Fn()会返回"object",typeof new Array()会返回"object"。然而,typeof不能准确判断null的数据类型,null会被判断为"object"。此外,jQuery也提供了一系列工具方法来判断数据类型,例如jQuery.isArray()用于判断是否为数组,jQuery.isEmptyObject()用于判断是否为空对象,jQuery.isFunction()用于判断是否为函数等等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [JS判断数据类型的5种方法](https://blog.csdn.net/weixin_45571121/article/details/120518849)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [js判断数据类型(全)](https://blog.csdn.net/qq_30136729/article/details/124090314)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值