JavaScript判断数据类型的几种方法详解

作者:青柠

时间:2026-10-23

浏览:556

本文详解JavaScript中判断数据类型的三种常用方法:typeof、Object.prototype.toString.call以及constructor属性。通过对比分析,指出typeof在区分对象、数组和null时的局限性,并展示如何利用Object.prototype.toString实现精确类型判断,最后提供封装工具类的代码示例。

文章目录

function judgeType(change) {
    if (arguments.length == 0) {
      return '0';//无参数传入
    }
    if (change === null) {
      return 'null'
    }
    if (change === undefined && arguments.length > 0) {
      return 'undefined'
    }
    if (change instanceof Function) {
      return 'function'
    }
    if (change instanceof Array) {
      return 'arry'
    }
    if (change instanceof Number || typeof change == 'number') {
      return 'number'
    }
    if (change instanceof String || typeof change == 'string') {
      return 'string'
    }
    if (change instanceof Boolean || typeof change == 'boolean') {
      return 'boolean'
    }
  }

ps:下面看下js 判断各种数据类型

使用typeof判断数据类型

在JavaScript开发中,我们常使用typeof操作符来检测变量的数据类型,其语法形式主要有两种:直接写typeof变量名,或者使用括号形式typeof(变量名)。

具体表现如下例所示:


 typeof  2   输出  number
    typeof  null  输出  object
    typeof  {}  输出  object
    typeof  []  输出  object
    typeof  (function(){})  输出 function 
    typeof  undefined     输出 undefined
    typeof  '222'         输出  string 
   typeof true          输出   boolean 

上述方式涵盖了JavaScript中的五种基本数据类型:number、string、boolean、undefined以及object,此外还能识别function类型。

此时读者可能会产生疑问:既然typeof能识别对象,那如何进一步区分普通对象、数组以及null值呢?

利用Object.prototype.toString精确区分

为此,我们需要引入另一个重要工具:Object.prototype.toString.call。

这是对象原型上的一个原生扩展方法,能够更精准地识别具体的数据类型。

让我们通过实际代码来测试这一方法的效果:


  var  gettype=Object.prototype.toString
    gettype.call('aaaa')    输出   [object String]
    gettype.call(2222)     输出   [object Number]
    gettype.call(true)     输出   [object Boolean]
    gettype.call(undefined) 输出   [object Undefined]
    gettype.call(null)         输出  [object Null]
     gettype.call({})          输出  [object Object]
     gettype.call([])          输出  [object Array]
     gettype.call(function(){})   输出  [object Function]

经过上述测试,之前关于对象、数组和null的区分问题便迎刃而解。

通过constructor属性判断

除了上述方法,constructor属性同样可用于判断数据类型:

例如:


''.constructor==String   
      [].constructor==Array
      var obj= new Object()  
  obj.constructor==Object

实际上,JavaScript中还存在针对各类DOM节点的类型判断,例如   [object HTMLDivElement]    表示div元素,[[object HTMLBodyElement]]表示body元素,[object Document](IE),或者[object HTMLDocument](firefox,google)等,这些判断在处理插件开发时非常实用。

封装类型判断工具方法

我们可以将相关逻辑封装为如下工具方法:


 var  gettype=Object.prototype.toString
   var  utility={
     isObj:function(o){
        return  gettype.call(o)=="[object Object]";
     },
     isArray:function(o){
        return  gettype.call(o)=="[object Array]";
     },
     isNULL:function(o){
        return  gettype.call(o)=="[object Null]";
     },
     isDocument:function(){
         return  gettype.call(o)=="[object Document]"|| [object HTMLDocument];
     }
     ........
  }

获取类型的函数还有一种更简洁的写法:


var Type = (function() {
        var type = {};
        var typeArr = ['String', 'Object', 'Number', 'Array','Undefined', 'Function', 'Null', 'Symbol'];
        for (var i = 0; i < typeArr.length; i++) {
          (function(name) {
            type['Is' + name] = function(obj) {
              return Object.prototype.toString.call(obj) == '[object ' + name + ']';
            }
          })(typeArr[i]);
        }
        return type;
})(); 

调用示例如下:Type.IsFunction(function() {})      Type.IsObject({})。

即Type.Is...系列方法。

总结

以上介绍了JavaScript中判断数据类型的几种函数实例,希望能对大家有所帮助。如有任何疑问,欢迎留言,小编会及时回复!

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章