zoukankan      html  css  js  c++  java
  • 推断js中的类型:typeof / instanceof / constructor / prototype

    怎样推断js中的类型呢,先举几个样例:

    var a = "jason";

    var b = 123;

    var c = true;

    var d = [1,2,3];

    var e = new Date();

    var f = function(){
        alert('jason');

    };


    一、最常见的推断方法:typeof

        typeof是一个一元运算符,它返回的结果始终是一个字符串,对不同的操作数,它返回不同的结果,另外typeof能够推断function的类型;在推断除Object类型的对象时比較方便。

    console.log(typeof a == "string");        //true

    console.log(typeof a == String);        //false
    详细的规则例如以下:

      1) 对于数字类型的操作数而言, typeof 返回的值是 number。比方说:typeof 1,返回的值就是number。

        上面是举的常规数字,对于很规的数字类型而言,其结果返回的也是number。比方typeof NaN,NaN在JavaScript中代表的是特殊非数字值,尽管它本身是一个数字类型。

        在JavaScript中,特殊的数字类型还有几种:

    Infinity                     //表示无穷大特殊值

    NaN                         //特殊的非数字值

    Number.MAX_VALUE             //可表示的最大数字

    Number.MIN_VALUE             //可表示的最小数字(与零最接近)

    Number.NaN                     //特殊的非数字值

    Number.POSITIVE_INFINITY    //表示正无穷大的特殊值

    Number.NEGATIVE_INFINITY    //表示负无穷大的特殊值

        以上特殊类型,在用typeof进行运算进,其结果都将是number。


      2) 对于字符串类型,typeof返回的值是string。比方typeof  "jason"返回的值是string。

      3) 对于布尔类型,typeof返回的值是boolean。比方typeof true返回的值是boolean。

      4) 对于对象、数组、null返回的值是object。比方typeof {},typeof [],typeof null返回的值都是object。


      5) 对于函数类型,返回的值是function。比方:typeof eval,typeof Date返回的值都是function。

      6) 假设运算数是未定义的(比方说不存在的变量、函数或者undefined),将返回undefined。比方:typeof jason、typeof undefined都返回undefined。

    console.log(typeof a);                    //string

    console.log(typeof b);                    //number

    console.log(typeof c);                    //boolean

    console.log(typeof d);                    //object

    console.log(typeof e);                    //object

    console.log(typeof f);                    //function

    console.log(typeof 1);                    //number

    console.log(typeof NaN);                //number

    console.log(typeof Number.MIN_VALUE);    //number

    console.log(typeof Infinity);            //number

    console.log(typeof "123");                //string

    console.log(typeof true);                //boolean

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

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

    console.log(typeof null);                //object

    console.log(typeof eval);                //function

    console.log(typeof Date);                //function

    console.log(typeof sss);                //undefined

    console.log(typeof undefined);            //undefined


    二、推断一个对象是否为某一数据类型,或一个变量是否为一个对象的实例:instanceof

        注意:instanceof 后面一定要是对象类型,而且大写和小写不能错,该方法适合一些条件选择或分支。

    console.log(d instanceof Array);        //true

    console.log(e instanceof Date);            //true

    console.log(f instanceof Function);        //true


    三、依据对象的constructor推断:constructor

    console.log(d.constructor === Array)    //true

    console.log(e.constructor === Date)        //true

    console.log(f.constructor === Function)    //true

    注意constructor在类继承时会出错

    比如:

        function A(){};

        function B(){};

        var aObj = new A();

        console.log(aObj.constructor === A);    //true;

        console.log(aObj.constructor === B);    //false;
        

        function C(){};

        function D(){};

        C.prototype = new D(); //C继承自D

        var cObj = new C();

        console.log(cObj.constructor === C);    //false;

        console.log(cObj.constructor === D);    //true;
        
    而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:

        console.log(cObj instanceof C);            //true

        console.log(cObj instanceof D);            //true
        

    解决construtor的问题一般是让对象的constructor手动指向自己:

        cObj.constructor = C;                     //将自己的类赋值给对象的constructor属性

        console.log(cObj.constructor === C);    //true;

        console.log(cObj.constructor === D);    //false; 基类不会报true了;
        
        
    四、通用但非常繁琐的方法:prototype    

        console.log(Object.prototype.toString.call(a) === '[object String]');        //true

        console.log(Object.prototype.toString.call(b) === '[object Number]');        //true

        console.log(Object.prototype.toString.call(c) === '[object Boolean]');        //true

        console.log(Object.prototype.toString.call(d) === '[object Array]');        //true

        console.log(Object.prototype.toString.call(e) === '[object Date]');            //true

        console.log(Object.prototype.toString.call(f) === '[object Function]');        //true

        注:大写和小写不能写错,比較麻烦,但胜在通用。


    总结:

        通常情况下用typeof推断就能够了,遇到预知Object类型的情况能够选用instanceof或constructor方法,简单总结下,欢迎补充!


  • 相关阅读:
    No resource found that matches the given name 'Theme.AppCompat.Light' 的完美解决方案
    Data Flow ->> Fuzzy Lookup & Fuzzy Grouping
    SSIS ->> Script Debugging and Troubleshooting
    Data Flow ->> Script Component
    SSIS ->> Logging
    SSIS ->> Event Handler
    SSIS ->> Script Task
    Data Flow ->> Look up & Merge Join
    SSIS ->> 生成时间格式
    SSIS ->> Null & Null Functions
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4007275.html
Copyright © 2011-2022 走看看