zoukankan      html  css  js  c++  java
  • js 判断对象类型

    在企业级的开发中,我们常用 typeof 来判断企业 对象类型;但是 typeof 不能判断 Array 和 null

    这里我们使用一个 原型上的 toString方法;请看一下代码:

     1 <script>
     2     var allType = {
     3         "[object Array]" : "Array",
     4         "[object String]" : "String",
     5         "[object Function]" : "Function",
     6         "[object Boolean]" : "Boolean",
     7         "[object Number]" : "Number",
     8         "[object Object]" : "Object",
     9         "[object Undefined]" : "Undefined"
    10     };
    11     function MyTypeOf(obj) {
    12         if (obj === null) {
    13             return "null";
    14         }
    15         var res = Object.prototype.toString.call(obj);//使用 Object 原型链上的 toString 方法
    16         return allType[res];
    17     }
    18 
    19     function myTypeOf(obj) {
    20 
    21         var type = typeof (obj);
    22         if (obj === null) {
    23             return "null";
    24         } else if (type === "object") {
    25             var res = Object.prototype.toString.call(obj);//使用 Object 原型链上的 toString 方法
    26             return allType[res];
    27         }else {
    28             return type
    29         }
    30 
    31     }
    32 
    33     var a = [ 1, 2, 3 ];
    34     var b = "aa";
    35     var c = function() {
    36     };
    37     var d = true;
    38     var e = 1;
    39     var f = {};
    40     var g = undefined;
    41     var h = null;
    42 
    43     console.log(MyTypeOf(a));
    44     console.log(MyTypeOf(b));
    45     console.log(MyTypeOf(c));
    46     console.log(MyTypeOf(d));
    47     console.log(MyTypeOf(e));
    48     console.log(MyTypeOf(f));
    49     console.log(MyTypeOf(g));
    50     console.log(MyTypeOf(h));
    51     console.log("******************************************")
    52     console.log(typeof (a));//object Array失败
    53     console.log(typeof (b));//string String 成功
    54     console.log(typeof (c));//function function 成功
    55     console.log(typeof (d));//boolean  boolean 成功
    56     console.log(typeof (e));//number  number 成功
    57     console.log(typeof (f));//object  object 成功
    58     console.log(typeof (g));//undefined undefined 成功
    59     console.log(typeof (h));//object  null 失败
    60     console.log("******************************************");
    61     console.log(myTypeOf(a));
    62     console.log(myTypeOf(b));
    63     console.log(myTypeOf(c));
    64     console.log(myTypeOf(d));
    65     console.log(myTypeOf(e));
    66     console.log(myTypeOf(f));
    67     console.log(myTypeOf(g));
    68     console.log(myTypeOf(h));
    69 </script>

    myTypeOf 函数 可以实现 判断对象的类型;这里使用了原型链上的toString的方法:Object.prototype.toString.call(obj);来实现类型的判断

    myTypeOf和 MyTypeOf 实现的功能是一样的;myTypeOf略有优化

  • 相关阅读:
    C# 图片与Base64的相互转化
    LeetCode 303. Range Sum Query – Immutable
    LeetCode 300. Longest Increasing Subsequence
    LeetCode 292. Nim Game
    LeetCode 283. Move Zeroes
    LeetCode 279. Perfect Squares
    LeetCode 268. Missing Number
    LeetCode 264. Ugly Number II
    LeetCode 258. Add Digits
    LeetCode 257. Binary Tree Paths
  • 原文地址:https://www.cnblogs.com/lixiuming521125/p/10977791.html
Copyright © 2011-2022 走看看