JavaScript 数据类型检测大全

JavaScript 数据类型

1.基本数据类型检测

            var str = "test";
            var num = 1;
            var bool = true;
            var undef = undefined;
            var nul = null;
            var obj = {
                a:1
            }
            var f = function(){
                console.log("test");
            }


            console.log(typeof str);         //"string"
            console.log(typeof num);         //"number"
            console.log(typeof bool);        //"boolean"    
            console.log(typeof undef);       //"undefined"
            console.log(typeof nul);         //"object"
            console.log(typeof obj);         //"object"
            console.log(typeof f);           //"function"

2.引用类型的数据检测


                var arr = [];
                var date = new Date();
                var reg = /[0-9]/; 
                var f = function(){
                    console.log("test");
                }

                var PackStr = new String("test");
                var PackNum = new Number(10);
                var PackBool = new Boolean(true);
 

         (1) instanceof 检测

                console.log(arr instanceof Array);              //true          
                console.log(date instanceof Date);              //true
                console.log(reg instanceof RegExp);             //true
                console.log(f instanceof Object);               //true
                console.log(PackStr instanceof String);         //true
                console.log(PackNum instanceof Number);         //true
                console.log(PackBool instanceof Boolean);       //true

        (2) 构造函数检测

                console.log(arr.constructor == Array);          //true       
                console.log(date.constructor ==  Date);         //true
                console.log(reg.constructor == RegExp);         //true
                console.log(f.constructor == Function);         //true
                console.log(PackStr.constructor == String);     //true
                console.log(PackNum.constructor == Number);     //true
                console.log(PackBool.constructor == Boolean);   //true

       (3) 原型检测

                console.log(arr.__proto__ == Array.prototype);         //true
                console.log(date.__proto__ ==  Date.prototype);        //true
                console.log(reg.__proto__ == RegExp.prototype);        //true
                console.log(f.__proto__ == Function.prototype);        //true
                console.log(PackStr.__proto__ == String.prototype);    //true
                console.log(PackNum.__proto__ == Number.prototype);    //true
                console.log(PackBool.__proto__ == Boolean.prototype);  //true


                console.log(Object.getPrototypeOf(arr) == Array.prototype);         //true       
                console.log(Object.getPrototypeOf(date) ==  Date.prototype);        //true
                console.log(Object.getPrototypeOf(reg) == RegExp.prototype);        //true
                console.log(Object.getPrototypeOf(f) == Function.prototype);        //true
                console.log(Object.getPrototypeOf(PackStr) == String.prototype);    //true
                console.log(Object.getPrototypeOf(PackNum) == Number.prototype);    //true
                console.log(Object.getPrototypeOf(PackBool) == Boolean.prototype);  //true


                console.log(Array.prototype.isPrototypeOf(arr));          //true
                console.log(Date.prototype.isPrototypeOf(date));          //true
                console.log(RegExp.prototype.isPrototypeOf(reg));         //true
                console.log(Function.prototype.isPrototypeOf(f));         //true
                console.log(String.prototype.isPrototypeOf(PackStr));     //true
                console.log(Number.prototype.isPrototypeOf(PackNum));     //true
                console.log(Boolean.prototype.isPrototypeOf(PackBool));   //true


       (4) Object.prototype.toString 检测

                console.log(Object.prototype.toString.call(arr) == "[object Array]");       //true
                console.log(Object.prototype.toString.call(date) == "[object Date]");       //true
                console.log(Object.prototype.toString.call(reg) == "[object RegExp]");      //true
                console.log(Object.prototype.toString.call(f) == "[object Function]");      //true
                console.log(Object.prototype.toString.call(PackStr) =="[object String]");   //true
                console.log(Object.prototype.toString.call(PackNum) =="[object Number]");   //true
                console.log(Object.prototype.toString.call(PackBool) =="[object Boolean]"); //true

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值