• js中类型识别的方法


    第一种方法typeof

    typeof是一种运算符,它的值有以下几种

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>ces</title>
    </head>
    <body>
        <script>
        console.log(typeof "Liesbeth");//"string"
        console.log(typeof 12);//"number"
        console.log(typeof true); //"boolean"
        console.log(typeof undefined);//"undefined"
        console.log(typeof null);//"object"
        console.log(typeof {name:"Liesbeth"});//"object"
        console.log(typeof function(){});//"function"
        console.log(typeof []);//"object"
        console.log(typeof new Date);//"object"
        console.log(typeof /[0-9]/);//'object'
        console.log(typeof new Number(123));//'object'
        function Person(){};
        console.log(typeof new Person);//'object'
    
        </script>
    
    </body>
    </html>
    typeof

    运行结果如下,可以看出typeof能判断基本类型的值,其他通过new创建的类型除了function外,其他都是object,其中null也是object

    第二种方法instanceof

    instanceof运算符可以用来判断某个构造函数的prototype属性所指向的對象是否存在于另外一个要检测对象的原型链上即某个对象是否是某个构造函数的实例,如下

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>ces</title>
    </head>
    <body>
        <script>
        //能判别内置对象内型
        console.log([] instanceof Array);//true
        console.log([] instanceof Object);//true    
        console.log(/[0-9]/ instanceof RegExp);//true
        console.log(new String('123') instanceof String);//true
        console.log(function(){} instanceof Function);//true
    
        //不能判断原始类型
        console.log('123' instanceof String);//false
        console.log(123 instanceof Number);//false
        console.log(true instanceof Boolean);//false
    
       //判别自定义对象类型
        function Point(x,y){
            this.x=x;
            this.y=y;
        }
        function Circle(x,y,r){
            Point.call(this,x,y);
            this.radius=r;
        }
        Circle.prototype=new Point();
        Circle.prototype.constructor=Circle;
        var c= new Circle(1,1,2);
        console.log(c instanceof Circle);//true
        console.log(c instanceof Point);//true
        </script>
    
    </body>
    </html>
    instanceof

    运行结果如下图

    由此可看出instanceof不能识别原始类型,能识别自定义对象和内置对象类型同时它们都属于Object

     第三种方法Object.prototype.toString.call

    这个方法获取的是对象的[[Class]]属性值,返回结果是固定的'[object'+[[class]]属性+']',jquery中的$.type()就是利用这个属性判断对象类型的。

    function type(obj){
      return Object.prototype.toString.call(obj).slice(8,-1);
    }

    运行结果如下:

    其中的type(c)为instanceof时创建的对象。

    可以看出toString能识别原始类型和内置对象,但是不能识别自定义对象类型,另外在ie7和8中null和undefined都是返回"Object"如下

    同时在IE6中字符串也会返回"Object"所以用的时候要多注意这几个值。

    第四种方法constructor

    constructor 属性返回对创建此对象的数组函数的引用,如图所示

    利用此特性我们可以判别类型,其中由于null和undefined没有构造函数所以特殊处理,编写函数如下

    function getConstructorName(obj){
        return (obj===undefined||obj===null)?obj:(obj.constructor&&obj.constructor.toString().match(/functions*([^(]*)/)[1]);
    }

     运行结果如下

    可以看出constructor能判别原始类型(除null和undefined),内置对象和自定义对象.

    参考资料:http://www.cnblogs.com/ziyunfei/archive/2012/11/05/2754156.html其中对Object.prototype.toString解释的清楚。

     

  • 相关阅读:
    计算数组的逆序对个数
    处理类型(typedef,uisng,auto,decltype)
    constexpr与常量表达式(c++11标准)
    const的限定
    void*类型的指针
    linux终端拖动鼠标总是产生ctrl+c
    Linux hrtimer分析(2)
    Linux hrtimer分析(一)
    Alarm(硬件时钟) init
    第十一章 Android 内核驱动——Alarm
  • 原文地址:https://www.cnblogs.com/liziyu91/p/5689960.html
Copyright © 2020-2023  润新知