网站首页 文章专栏 原生javascript数据类型判断
原生javascript数据类型判断
编辑时间:2019-04-11 23:52:07 作者:lizhi 浏览量:316
<script>
        var a = 'string';
        var b = 1;
        var c = true;
        var f = function () {
            return a;
        }
        var g = null
        var h = undefined
        var i = Symbol()
        var j = new Array()
        var k = new Date()
        var l = new RegExp()
        var m = new Object()
        var n = new Map()
        var e = new Error()
       
        var index = 1
        if(index === 1) {
            // typeof 能判断的七种类型,首字母小写  string number boolean function undefined symbol object
            console.log ( typeof a)
            console.log ( typeof b)
            console.log ( typeof c)
            console.log ( typeof f)
            console.log ( typeof h)
            console.log ( typeof i)
            console.log('--------')
            console.log ( typeof g)  
            console.log ( typeof j)
            console.log ( typeof k)
            console.log ( typeof l)
            console.log ( typeof m)
            console.log ( typeof n)
            console.log ( typeof e)

        } else if (index === 2) {
            console.log (f instanceof Function)
            // instanceof 只能判断七种数据类型,不能判断null,首字母大写  Array Date RegExp Object Function Map
            console.log (a instanceof String)
            console.log (b instanceof Number)
            console.log (c instanceof Boolean)
            console.log (g instanceof Object)  
            console.log (h instanceof Object)
            console.log (i instanceof Symbol)
            console.log('--------')
            console.log (j instanceof Array)
            console.log (k instanceof Date)
            console.log (l instanceof RegExp)
            console.log (m instanceof Object)
            console.log (f instanceof Function)
            console.log (n instanceof Map)
            console.log (e instanceof Error)
        } else if (index === 3) {
            // constructor无法判断 undefined  null
            Math.floor(9).constructor === Number
            [1,2,3].constructor === Array
            true
            '11'.constructor === String
            true
            true.constructor === Boolean
            true
            var fn = function() { return 1}
            undefined
            fn.constructor === Function
            true
            Symbol().constructor === Symbol
            true
            new Array().constructor === Array
            true
            new Date().constructor === Date
            true
            new RegExp().constructor === RegExp
            true
            new Object().constructor === Object
            true
            new Map().constructor === Map
            true
            new Error().constructor === Error
            true
        }

        // 判断是否为空
        function isNull(obj) {
            return typeof obj === 'object' && !(obj instanceof Object) && obj === null
        }
        // 推荐使用 万能数据类型判断
        function getObjectType(obj){
            return Object.prototype.toString.call(obj).slice(8,-1);
        }
        console.log(getObjectType(n))
        
    </script>
来说两句吧
最新评论