js中判断数据类型的方法

一.typeof

二.instanceof

三.通过Object.prototype.toString


一.typeof

语法:         typeof 需要判断的数据类型

        console.log(typeof 123);                        //number
        console.log(typeof '王小美');                    //string
        console.log(typeof true);                       //boolean
        console.log(typeof undefined);                  //undefined
        console.log(typeof null);                       //object
        console.log(typeof [123, 4]);                   //object
        console.log(typeof function () { });            //function
        console.log(typeof { name: '王小美' });          //object

可以看出判断的基本上是没有什么太大问题的,但是判断null时返回的却是object

所以这是typeof的缺点还是优点呢?说缺点的话这个null归类于object确实有点不合适

说优点的话只有typeof判断null的时候返回的是object哈哈哈哈

判断数组的时候返回的也是object,但数组是Array


二.instanceof

语法:         对象 instanceOf 构造函数名

作用:         判断这个复杂数据类型的prototype在不在这个对象的原型链上

返回布尔值

        let arr = [10, 20, 30];
        let str = {}
        let str2 = '王小美';
        let fn = function () { }
        console.log(arr instanceof Array);       //true
        console.log(arr instanceof Object);      //true
        console.log(fn instanceof Function);     //true
        console.log(fn instanceof Object);       //true
        console.log(str instanceof Object);      //true
        console.log(str instanceof Function);    //false
        console.log(str2 instanceof String);     //返回false 判断不出来

        // 根据原型链去查找 根据instanceof的语法来判断的话前面的Object会被看成对象
        // 结果都为true
        console.log(Object instanceof Object);
        console.log(Object instanceof Function);
        // 都为true
        console.log(Function instanceof Function);
        console.log(Function instanceof Object);

三.通过Object.prototype.toString

万能数据监测[object type]type是数据类型

 let str = { name: '王小美' }
 //对象调用toString()方法时返回[object type];其他数据类型调用toString时会返回其他内容
 console.log(str.toString()); //[object Object]

这个方法是在Object原型上的方法,如果要使用的话就要改变这个方法的this指向,借用一下,具体就是让这个方法指向我们输入的数据类型

        // call()和apply,bind方法都可以
        console.log(Object.prototype.toString.call(123));                                       // [object Number]
        console.log(Object.prototype.toString.call('abc'));                                     // [object String]
        console.log(Object.prototype.toString.call(true));                                      // [object Boolean]
        console.log(Object.prototype.toString.call(undefined))                                  // [object Undefined]
        console.log(Object.prototype.toString.call(null));                                      // [object Null]
        console.log(Object.prototype.toString.call([123, 4]))                                   // [object Array]
        console.log(Object.prototype.toString.apply([123, 4]))                                  // [object Array]
        console.log(Object.prototype.toString.call(function () { }))                            // [object Function]
        console.log(Object.prototype.toString.call({ name: '王小美' }))                         // [object Object]

结果在右边,底部有滚动条

上面就是我所知的一些判断数据类型的方法 ,如果有其他方法可以评论哈,或者发现我有错误的话谢谢指出哈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Motion_zq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值