javascript检测基本类型值或引用类型值的类型方法

首先javascript的数据类型分为两种数据类型:基本数据数据类型和引用数据类型

基本数据类型:Number,String,Boolean,Undefined,Null。原始值,是简单的数据段,可按值访问,直接操作保存在变量中的实际值

引用数据类型:Object,Array,Function,Date,RegExp,单体内置对象(Math,window(客户端才有的全局对象),global(服务端才有的全局对象))。引用值,是保存在栈内存中的对象,是有多个值构成的对象,与其他语言不同的是:你不可以直接访问堆内存空间中的位置和操作堆内存空间,只能操作对象在堆内存中的引用地址

我在这列举了3种方法,使用console.log()逐一打印出来,一目了然

 

第1种方法,使用typeof检测:

检测类型typeof,typeof(x)或者typeof x:检测基本类型值的数据类型,不建议检测引用类型值(note:也可检测单体内置对象(Math,window,global))
      console.log(typeof 1); //number
      console.log(typeof "1"); //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
      console.log(typeof new Date()); //object
      console.log(typeof new RegExp()); //object
      console.log(typeof Math); //object
      console.log(typeof window); //object(客户端的全局对象,否则undefined)
      console.log(typeof global); //object(服务端的全局对象,否则undefined)

第2种方法:使用instanceof检测:

检测类型instanceof:检测一个引用类型值是什么类型的对象,不建议检测基本类型值,因为在基本类型值中始终返回false(note:不可检测单体内置对象(Math,window,global))
      console.log(1 instanceof Number); //false
      console.log("1" instanceof String); //false
      console.log(true instanceof Boolean); //false
      // console.log(undefined instanceof Undefined); //报错,因为Undefined不存在
      // console.log(null instanceof Null); //报错,因为Undefined不存在

      console.log({} instanceof Object); //true
      console.log([] instanceof Array); //true,也属于Object类型
      console.log(function() {} instanceof Function); //true,也属于Object类型
      console.log(new Date() instanceof Date); //true,也属于Object类型
      console.log(new RegExp() instanceof RegExp); //true,也属于Object类型

 第3种方法:使用Object.prototype.toString().call(x)检测:

扫描二维码关注公众号,回复: 5140264 查看本文章
检测类型Object.prototype.toString.call(x),可检测基本类型值和引用类型值(note:也可检测单体内置对象(Math,window,global)和JSON)
但是Object.prototype.toString()本身是允许被修改的,而我们目前所探讨的是假设未被修改为前提的
      console.log(Object.prototype.toString.call(1)); //[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({})); //[object Object]
      console.log(Object.prototype.toString.call([])); //[object Array]
      console.log(Object.prototype.toString.call(function(){})); //[object Function]
      console.log(Object.prototype.toString.call(new Date())); //[object Date]
      console.log(Object.prototype.toString.call(new RegExp())); //[object RegExp]

      console.log(Object.prototype.toString.call(Math)); //[object Math]
      console.log(Object.prototype.toString.call(window)); //[object window](客户端的全局对象,否则报错)
      // console.log(Object.prototype.toString.call(global)); //[object global](服务端的全局对象,否则报错)

      console.log(window.JSON && Object.prototype.toString.call(JSON)); // [object JSON]
 
使用这种方法,可以编写一个判断所有类型的函数:
      function getType(obj){
        let str = Object.prototype.toString.call(obj); // 检测基本类型值,引用类型值的类型
        let map = {
          '[object Boolean]': 'boolean',
          '[object Number]': 'number',
          '[object String]': 'string',
          '[object Function]': 'function',
          '[object Array]': 'array',
          '[object Date]': 'date',
          '[object RegExp]': 'regExp',
          '[object Undefined]': 'unfefined',
          '[object Null]': 'null',
          '[object Object]': 'object'
        };
        if(obj instanceof Element){
          return 'element';
        }
        return map[str];
      }
      console.log("string"); // string
 

猜你喜欢

转载自www.cnblogs.com/jinbang/p/10346584.html
今日推荐