我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

jsvascript判断数据类型

  1. 代码:
    function getType(val){
    this.val = val
    this.isInt = function(val){
        if((typeof(val)=='number')||(val instanceof Number)){
            if(/^\d*$/.test(val)){
                return true
            }else{
                return false
            }
        }else{
            return false
        }
    }
    this.isFloat = function(val){
        if((typeof(val)=='number')||(val instanceof Number)){
            if(/^\d*\.\d*$/.test(val)){
                return true
            }else{
                return false
            }
        }else{
            return false
        }
    }
    this.isString = function(val){
        if((typeof(val)=='string')||(val instanceof String)){
            return true
        }else{
            return false
        }
    }
    this.isBoolean = function(val){
        if((typeof(val)=='boolean')||(val instanceof Boolean)){
            return true
        }else{
            return false
        }
    }
    this.isArray = function(val){
        if(val instanceof Array){
            return true
        }else{
            return false
        }
    }
    this.isObject = function(val){
        if(val instanceof Object && typeof val.length == 'undefined'){
            return true
        }else{
            return false
        }
    }
    this.dump = function(){
        var val = this.val
        var isint = this.isInt(val)
        var isfloat = this.isFloat(val)
        var isstring = this.isString(val)
        var isbool = this.isBoolean(val)
        var isarray = this.isArray(val)
        var isobject = this.isObject(val)
        if(isint){
            return 'int'
        }else if(isfloat){
            return 'float'
        }else if(isstring){
            return 'string'
        }else if(isbool){
            return 'boolean'
        }else if(isarray){
            return 'array'
        }else if(isobject){
            return 'object'
        }
    }
    }
    var a = new getType(1)
    var aa = a.dump()
    var b = new getType(1.1)
    var bb = b.dump()
    var c = new getType('hello')
    var cc = c.dump()
    var d = new getType(true)
    var dd = d.dump()
    var e = new getType([1,2])
    var ee = e.dump()
    var f = new getType({name:'lee'})
    var ff = f.dump()
    console.log(aa)
    console.log(bb)
    console.log(cc)
    console.log(dd)
    console.log(ee)
    console.log(ff)
  2. 输出:
    int
    float
    string
    boolean
    array
    object

当前名称:jsvascript判断数据类型
本文地址:http://shouzuofang.com/article/ihdgjp.html

其他资讯