javascript数据类型判断的几种方法

数据类型的判断
有时我们经常需要判断数据的类型,有时又不清楚使用具体的方法。这里总结了一下集中的数据类型判断的方法 总有一款适合你

1. typeof (先抛结论):对于基本数据类型判断是没有问题的,但是遇到引用数据类型(如:array object) 是不起作用 其中对 array object null 返回的结果都是object 以下是具体的代码实例

    var test1=null
    var test2=23
    var test3=false
    var test4='gri'
    var test5=[1,2,3]
    var test6={
      name: 'zs',
      age: 23
    }
     var test7=undefined
    console.log('test1的数据类型是' + typeof test1 ); // object
    console.log('test2的数据类型是' + typeof test2 ); // number
    console.log('test3的数据类型是' + typeof test3);  // boolean
    console.log('test4的数据类型是' + typeof test4 ); // string
    console.log('test5的数据类型是' + typeof test5 ); // object
    console.log('test6的数据类型是' + typeof test6 ); // object
    console.log('test7的数据类型是' + typeof test7 ); // undefined

2. instanceof 可以检测array,object但是不能检测基本类型的数据也包括null也不可以
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。

       // 1. instanceof 用来检测某个对象是不是另一个对象的实例 
      // Car是一个汽车的构造函数
      function Car (model,year) {
        this.model=model;
        this.year=year;
      }
      // Person是一个人的构造函数
      function Person(name, age) {
        this.name=name;
        this.age=age; 
      }
      // 可以判断实例化对象来自那个构造函数
      const bwm=new Car('宝马','2020');
      console.log(bwm instanceof Car); // true
      console.log(bwm instanceof Person); // false
      console.log(Car);
      console.log(Person);
      console.log(bwm); //在实例化对象bwm的原型链上存在构造函数Car的prototype属性
      // 2. instanceof 用于判断一个变量是否某个对象的实例,
	  var arr=new Array();
      var obj= {
        name:'zs',
        age:23
      }
      console.log(arr instanceof Array); //true
      console.log(arr instanceof Object); //true
      console.log(obj instanceof Object); //true
//注意如果需要判断数据类型存在数组和对象应该向判断数组如果先判断对象的话数组也是对象两者是没办法分开的

3. constructor 不能判断undefined和null,并且使用它是不安全的,因为contructor的指向是可以改变的

var bool= false
      var num= 123
      var str='zs' 
      var arr=[1,2,4]
      var obj={
        name:'gy',
        age:12
      }
      function fun() {}
      function Student(name,age,grades) {
        this.age=age
        this.name=name
        this.grades=grades
      }
      function Person(name,age) {
        this.age=age
        this.name=name
      }
      var gy=new Person('gy',23)
      console.log(bool.constructor === Boolean); // true
      console.log(num.constructor === Number);// true
      console.log(str.constructor === String);// true
      console.log(arr.constructor === Array);// true
      console.log(obj.constructor === Object);// true
      console.log(fun.constructor === Function);// true
      console.log(gy.constructor === Student);// false
      console.log(gy.constructor === Person);// true

4. 判断数据的最终的完美解决方案(Object.prototype.toString.call())

    function foo() {}
    console.log(Object.prototype.toString.call(1));   //'[object Number]'
    console.log(Object.prototype.toString.call(NaN));  //'[object Number]'
    console.log(Object.prototype.toString.call('1'));  //'[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(Symbol())); //'[object Symbol]'
    console.log(Object.prototype.toString.call(foo));   //'[object Function]'
    console.log(Object.prototype.toString.call([1,2,3]));  //'[object Array]'
    console.log(Object.prototype.toString.call({})); //'[object Object]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

疯狂平头哥前端乐园

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

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

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

打赏作者

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

抵扣说明:

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

余额充值