ITPub博客

首页 > IT职业 > IT职场 > js 中精确判断对象类型 -- 关于 typeof 和 Object.

js 中精确判断对象类型 -- 关于 typeof 和 Object.

IT职场 作者:tonydeck 时间:2021-09-09 10:08:34 0 删除 编辑

在 javascript 中可以使用 typeof 来判断数据类型,但 typeof 只能判断区分基本类型,即 number、string、boolean、undefinded 和 object 这 5 种;

    <script type="text/javascript">
        //基本数据类型:number、string、boolean、null、undefined
        //复杂数据类型(引用数据类型):object(Array、Data、RegExp、function)
        alert(typeof 1);  //number;
        alert(typeof true);  //boolean;
        alert(typeof undefined);  //undefined;
        alert(typeof "hello world")   //string;

        alert(typeof {});  //object;
        alert(typeof null); //object,  null是一个空对象;
        alert(typeof function(){});   //function;
    </script>

如上:对于 null(空对象)、数组、{}(对象)使用 typeof 判断数据类型,都会统一返回 “object” 字符串;

要精确判断对象类型可以使用 js 中的 Object.prototype.toString 方法(判断对象属于那种内置对象类型);

eg:

var arr=[];console.log(Object.prototype.toString.call(arr));  //结果是  "[object Array]"

在 ES3 中,Object.prototype.toString 方法的在被调用的时候,会执行如下的操作步骤:

  1. 获取 this 对象的 [[Class]] 属性的值;

  2. 计算出 “[object”+” 第一步获取的属性值”+“]” 这 3 个字符串拼接后的新字符串;

  3. 返回第 2 步计算出的新字符串;

其过程简单说来就是:

  1. 获取对象的类名(对象类型)。

  2. 然后将 [object、获取的对象类型的名、] 组合为字符串

  3. 返回字符串 “[object Array]” 。

[[Class]]是一个内部属性, 所有的对象 (原生对象和宿主对象) 都拥有该属性. 在规范中,[[Class]]是这么定义的:
内部属性,[[Class]] 一个字符串值, 表明了该对象的类型。

由于 JavaScript 中一切都是对象,任何都不例外,对所有值类型应用 Object.prototype.toString.call()方法结果如下:

    <script type="text/javascript">
        console.log(Object.prototype.toString.call(123))    //"[object Number]"
        console.log(Object.prototype.toString.call('123'))    //"[object String]"
        console.log(Object.prototype.toString.call(undefined))    //"[object Undefined]"
        console.log(Object.prototype.toString.call(true))    //"[object Boolean]"
        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]"
    </script>

如上可以看到:Object.prototype.toString.call()可以精确的判断对象类型;

判断是否为函数

   function isFunction(it) {        return Object.prototype.toString.call(it) === '[object Function]';
    }

有兼容性问题,全面的写法是:

 function isArray(arr){  //自己封装的一个函数isArray(),用于判断函数传入的参数是否是数组;
                if(typeof Array.isArray === "undefined"){  //Array.isArray()是ES5中新增的方法,先判断下其是否是undefined,如果是未定义的话执行该段函数,定义一个Array.isArray()方法;
                    Array.isArray = function(brr){  //如果未定义,则定义函数并传入参数;
                        return Object.prototype.toString.call(brr)=="[object Array]"  //通过js中的Object.prototype.toString方法,返回一个判断,假设参数对象值属于数组内置类型,判断时,如果返回true则是数组,否则不是;
                    }
                }                return Array.isArray(arr);  //返回该方法,调用isArray()相当于调用Array.isArray();
            }



作者:伍洋RobertWu
链接:https://www.jianshu.com/p/e037ab64c993


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4615/viewspace-2817311/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-13

  • 博文量
    84
  • 访问量
    963