数据类型的判断

`有时咱们常常须要判断数据的类型,有时又不分明应用具体的方法。这里总结了一下集中的数据类型判断的办法 总有一款适宜你`

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]