zoukankan      html  css  js  c++  java
  • js中判断对象数据类型的方法

    1.类型定义

    JS是一种弱类型语言。JS拥有动态类型,相同的变量可以用作不同的类型。
    JS有7种数据类型:三种基本类型(数字,字符串,布尔),两种引用数据类型(对象,数组),两种特殊数据类型(undefined,null)。
    JS有5种原始类型:数字,字符串,布尔,undefined,null。

    2.类型判断

    对js中不同数据的布尔值类型总结:false:空字符串;null;undefined;0;NaN。
    true:除了上面的false的情况其他都为true;

    如下:

    复制代码
    var o = { 
              'name':'lee'
            };
    var a = ['reg','blue'];
    function checkBoolean(a){
             if(a){
                  return true;
             }else{
                  return false;
             }
     }
    console.log(checkBoolean('')); //false
    console.log(checkBoolean(0)); //false
    console.log(checkBoolean(null)); //false
    console.log(checkBoolean(undefined)); //false
    console.log(checkBoolean(NaN)); //false
    console.log(checkBoolean(a));//true
    console.log(checkBoolean(c));//true
    复制代码

    javascript中有六种数据类型:string;boolean;Array;Object;null;undefined。如何检测这些数据类型呢,总结方法如下:

    方法一:采用typeof

    复制代码
           var fn = function(n){
              console.log(n);
           }
           var str = 'string';
           var arr = [1,2,3];
           var obj = {
               a:123,
               b:456
           };
           var num = 1;
           var b = true;
           var n = null;       var u = undefined;
           //方法一使用typeof方法。
           console.log(typeof str);//string
           console.log(typeof arr);//object
           console.log(typeof obj);//object
           console.log(typeof num);//number
           console.log(typeof b);//boolean
           console.log(typeof n);//null是一个空的对象
           console.log(typeof u);//undefined
           console.log(typeof fn);//function
    复制代码

    通过上面的检测我们发现typeof检测的Array和Object的返回类型都是Object,因此用typeof是无法检测出来数组和对象的,采用方法二和方法三则可以检测出来。

    方法二:instanceof

    复制代码
     var o = { 
               'name':'lee'
             };
     var a = ['reg','blue'];
     console.log(o instanceof Object);// true
     console.log(a instanceof Array);//  true
     console.log(o instanceof Array);//  false
    复制代码

     注意:instaceof只可以用来判断数组和对象,不能判断string和boolean类型,要判断string和boolean类型需要采用方法四。
     由于数组也属于对象因此我们使用instanceof判断一个数组是否为对象的时候结果也会是true。如:

    console.log(a instanceof Object);//true。

    下面封装一个方法进行改进:

    复制代码
    var o = { 
              'name':'lee'
            };
    var a = ['reg','blue'];
    var getDataType = function(o){
                if(o instanceof Array){
                    return 'Array'
                }else if( o instanceof Object ){
                    return 'Object';
                }else{
                    return 'param is no object type';
                }
           };
    console.log(getDataType(o));//Object。
    console.log(getDataType(a));//Array。
    复制代码

    方法三:使用constructor方法

    var o = { 
               'name':'lee'
            };
    var a = ['reg','blue'];
    console.log(o.constructor == Object);//true
    console.log(a.constructor == Array);//true

    方法四:利用tostring()方法,这个方法是最佳的方案。

    复制代码
    var o = { 
              'name':'lee'
            };
    var a = ['reg','blue'];
    function c(name,age){
             this.name = name;
             this.age = age;
     }
    var c = new c('kingw','27');
    console.log(Object.prototype.toString.call(a));//[object Array]
    console.log(Object.prototype.toString.call(o));//[Object Object]
    console.log(Object.prototype.toString.call(c));//[Object Function]
    console.log(Object.prototype.toString.call(new c));//[Object Object]
    //封装一个方法判断数组和对象
    function isType(obj){ var type = Object.prototype.toString.call(obj); if(type == '[object Array]'){ return 'Array'; }else if(type == '[object Object]'){ return "Object" }else{ return 'param is no object type'; } }
    console.log(isType(o));
    //Object console.log(isType(a));//Array

    //下面是更简洁的封装,来自vue源码
    var _toString = Object.prototype.toString;
    function toRawType (value) {return _toString.call(value).slice(8, -1)}
     
    复制代码

    方法五:利用jquery的$.isPlainObject();$.isArray(obj);$.isFunction(obj)进行判断。

     

    出处:https://www.cnblogs.com/xinggood/p/6568624.html

  • 相关阅读:
    windows下的IO模型之选择(select)模型
    tcp通讯中socket套接字accept和listen的关系
    转一篇shell中关于各种括号的讲解
    记两个std接口equal_range,set_difference
    nginx学习
    c++ 读取文本问题
    vim使用常看
    CNN设计一些问题
    什么是反射?反射机制的应用场景有哪些?
    java为什么只有值传递?
  • 原文地址:https://www.cnblogs.com/mq0036/p/12043898.html
Copyright © 2011-2022 走看看