js检测数据类型

1、typeof

  ①typeof对于基本数据类型来说,除了null都可以显示正确的类型;

  ②typeof对于对象来说,除了函数都会显示为Object,所以说typeof不能准确判断变量到底是什么类型,所以想判断一个对象的正确数据类型,这时候可以考虑instanceof;

  ③typeof可以判断基本数据类型(除了null以外 ),但是引用数据类型中除了function类型外,其他也无法判断

  ④例子:console.log( typeof 2 );                // number

      console.log( typeof 'str' );             // string

      console.log( typeof true );            // boolean

      console.log( typeof undefined );   // undefined

      console.log( typeof null );             // Object

      console.log( typeof [] );                //Object

      console.log( typeof {} );                //Object

      console.log( typeof function(){} ); // function
2、instanceof

  ①可以正确的判断对象的类型,因为内部机制是通过对象的原型链中是不是能找到类型的prototype

  ②instanceof可以准确的判断复杂的引用数据类型,但是不能判断基本数据类型

  ③例子:console.log( 2 instanceof Number );        //false

      console.log( 'str' instanceof String );        //false

      console.log( true instanceof Boolean );    //false

      console.log( [] instanceof Array );        //true

      console.log( {} instanceof Object );      //true

      console.log( function(){} instanceof Function );  //true

      undefined和null由于没有原型prototype所以使用instanceof检测时会报错( undefined/null is not defined )

3、constructor

  ①可以准确的判断基本数据类型(除undefined和null)以及引用数据类型;

  ②利用类和实例的关系,因为实例的constructor一般都等于类.prototype.constructor,也就是当前类本身(前提是原型的constructor没有被破坏)

  ③如果创建一个对象,更改它的原型,constructor就不建议使用了。

  ④例子:console.log( (2).constructor === Number );          //true

      console.log( ('str').constructor === String );          //true

      console.log( (true).constructor === Boolean );      //true

      console.log( ([]).contructor === Array );       //true

      console.log( ({}).constructor === Object );        //true

      console.log( (function(){}).constructor === Function );//true

  ⑤更改原型,例子:function Fn(){}

           Fn.prototype = new Array();

             var f = new Fn();

             console.log( f.constructor === Fn ); //false

             console.log( f.constructor === Array ); //true

4、Object.prototype.toString.call()

  ①toString()是Object的原型方法,调用该方法,可以统一返回格式'[Object,数据类型]'的字符串;

  ②对于Object对象,直接调用toString()就能返回[object Object];而对于其他对象就需要通过call调用,才能返回正确的类型信息。

  ③例子:Object.prototype.toString({});  //"[object Object]"

      Object.prototype.toString.call(1);  //"[object Number]"

      Object.prototype.toString.call('str');  //"[object String]"

      Object.prototype.toString.call(true);  //"[object Boolean]"

      Object.prototype.toString.call(null);  //"[object Null]"

      Object.prototype.toString.call(undefined);  //"[object Undefined]"

      Object.prototype.toString.call(function(){});  //"[object Function]"

      Object.prototype.toString.call([]);  //"[object Array]"

      Object.prototype.toString.call({});  //"[object Object]"

      Object.prototype.toString.call(/123/g);  //"[object RegExp]"

      Object.prototype.toString.call(new Date());  //"[object Date]"

      Object.prototype.toString.call(document);  //"[object HTMLDocument]"

      Object.prototype.toString.call(window);  //"[object Window]"

5、实现全局通用的数据类型判断( typeof + Object.prototype.toString.call() )

  function GetType( obj ){

    //使用typeof判断,如果是基本数据类型(number、string、boolean、undefined、function ),直接返回( 除了null ),注意:返回值首字母小写

    let type = typeof obj; 

 

    if( type  !== 'object' ){

      return type 

    }

    //对于typeof返回结果为object的在使用Object.prototype.toString.call()进行判断( null 、Array、Date、RegExp ),注意:返回值首字母大写

    return Object.ptototype.toString.call( obj ).replace(/^\[object (\S+)\]$/,'$1')

  }

posted @ 2022-07-08 13:45  我爱敲代码0000  阅读(42)  评论(0编辑  收藏  举报