• javascript中判断对象类型


     1 <script type="text/javascript">
     2         
     3         
     4         //判别一个对象属性在不在某个对象中
     5         //in 是用于查找某个属性是否存在于对象中,它会把对象
     6         //里面所有的内置方法全部返回
     7         
     8         var json = {username:"pipi"};
     9         console.log("username" in json);  //true
    10         console.log("toString" in json);  //true
    11         console.log("valueOf" in json);  //true
    12         console.log(json.constructor);  //function Object();区分对象具体类型
    13         
    14         console.log("=====================");
    15         var date = new Date();
    16         var arr = [];
    17         var obj = {};
    18         var regex = new RegExp();
    19         
    20         console.log(date.constructor);  //function Date()
    21         console.log(arr.constructor);  //function Array()
    22         console.log(obj.constructor);  //function Object()
    23         console.log(regex.constructor);  //function RegExp()
    24         
    25         /*
    26             判断是否为数组
    27         */
    28         function isArray(obj){
    29             if(obj && typeof obj === "object"){
    30                 var con = obj.constructor.toString();
    31                 return con.indexOf("Array") != -1 ? true : false;
    32             }
    33             return false;
    34         }
    35         
    36         function isArray1(obj){
    37             return typeof obj.sort === "function";
    38         }
    39         
    40         function isArray2(obj){
    41             return obj != null && typeof obj === "object" && "splice" in obj && "join" in obj && "length" in obj;
    42         }
    43         
    44         function isArray3(obj){
    45             return obj instanceof Array;
    46         }
    47         
    48         //ES5中提供的方法 IE678可能不支持
    49         console.log(Array.isArray(arr)); //true
    50         //自己定义
    51         //prototype当做对象的方法动态扩展
    52         Array.prototype.isArray = function(){
    53             return this instanceof Array;
    54         }
    55         
    56         
    57         console.log(isArray(arr));  //true
    58         console.log(isArray1(arr));  //true
    59         console.log(isArray2(arr));  //true
    60         console.log(isArray3(arr));  //true
    61         
    62         //判断是否为数字
    63         function isNumber(obj){
    64             var num = new Number(obj);
    65             return typeof obj === "number" && !("isNaN" in num);
    66         }
    67         
    68         //判断是否为空
    69         function isNull(obj){
    70             return obj === null;
    71         }
    72         
    73         //判断是否为undefined
    74         function isUndefined(obj){
    75             return obj === 0;
    76         }
    77         
    78         //判断是不是一个日期类型
    79         function isDate(obj){
    80             if(obj && typeof obj === "object"){
    81                 var con = obj.constructor.toString();
    82                 return con.indexOf("Date") != -1 ? true :false;
    83             }
    84             return false;
    85         }
    86         
    87         console.log(isDate(date));
    88         
    89
    /*
            prototype是js为每一个对象提供的原型链
            用于对象的动态扩展
        */
        Array.prototype.isArray = function(){
            return this instanceof Array;
        }
        
        String.prototype.startWith = function(key){
            return this.charAt(0) == key;
        }
        
        String.prototype.endWith = function(key){
            return this.charAt(this.length - 1) == key;
        }
    90         
    91     </script>
  • 相关阅读:
    探讨e.target与e.currentTarget
    JavaScript事件模型
    博客
    angular创建自定义指令的四种方式
    jqueryMobile模块整理—图标(icons)
    jqueryMobile模块整理—按钮(buttons)
    Visio 2010,如何打开多个窗口
    Ajax.ActionLink浏览器中代码解析问题
    响应式布局
    jquery的each
  • 原文地址:https://www.cnblogs.com/sun-rain/p/4851541.html
Copyright © 2020-2023  润新知