1

数据类型的判断

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

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]

疯狂平头哥
19 声望0 粉丝

happy coding happy cooking